PHP 中的字符串操作:首先拆分 space
String Manipulation in PHP: split on first space
在我的 PHP 5.3 应用程序中,我得到字符串
例如:ACTION data with lot of spaces
我需要将 ACTION 和 'data with lot of space' 作为两个字符串。
我行动不多。
如果我理解正确,您可以使用以下方法之一:
按空格分隔:list($action, $data) = explode(' ', $action_string, 2);
按正则表达式拆分:preg_match('/(\w+)\s(.*)/', $action_string, $matches);
($matches[1]
是action,$matches[2]
是rest data)
拆分并重新组合:$parts = explode(' ',$action_string); $action = array_shift($parts); $data = implode(' ', $parts);
这样使用explode()
,
print_R(explode(' ', 'ACTION data with lot of spaces', 2));
输出:
Array
(
[0] => ACTION
[1] => data with lot of spaces
)
观看演示 here
在我的 PHP 5.3 应用程序中,我得到字符串
例如:ACTION data with lot of spaces
我需要将 ACTION 和 'data with lot of space' 作为两个字符串。
我行动不多。
如果我理解正确,您可以使用以下方法之一:
按空格分隔:
list($action, $data) = explode(' ', $action_string, 2);
按正则表达式拆分:
preg_match('/(\w+)\s(.*)/', $action_string, $matches);
($matches[1]
是action,$matches[2]
是rest data)拆分并重新组合:
$parts = explode(' ',$action_string); $action = array_shift($parts); $data = implode(' ', $parts);
这样使用explode()
,
print_R(explode(' ', 'ACTION data with lot of spaces', 2));
输出:
Array
(
[0] => ACTION
[1] => data with lot of spaces
)
观看演示 here