我想知道是否有办法将一串短语拆分为一个数组。每个短语都用单引号括起来。我很感激任何建议!
我试过的
explode(' ', $string);
输入
$string = "'Hello world' 'green apples' 'red grapes'";
期望的输出
//ary[0] = 'Hello World'
//ary[1] = 'green apples'
//ary[2] = 'red grapes'
提前谢谢了!
我想知道是否有办法将一串短语拆分为一个数组。每个短语都用单引号括起来。我很感激任何建议!
我试过的
explode(' ', $string);
输入
$string = "'Hello world' 'green apples' 'red grapes'";
期望的输出
//ary[0] = 'Hello World'
//ary[1] = 'green apples'
//ary[2] = 'red grapes'
提前谢谢了!
更正$string
变量后
$string = "'Hello world' 'green apples' 'red grapes'";
$arr = explode("' '", trim($string, "'"));
print_r($arr);
它会输出:
Array
(
[0] => Hello world
[1] => green apples
[2] => red grapes
)
尝试这个 :
$string = "'Hello world' 'green apples' 'red grapes'";
preg_match_all("/'[\w\s]+'/",$string,$match);
echo "<pre>";
print_r($match[0]);
参考:http ://www.php.net/manual/en/function.preg-match-all.php
输出 :
Array
(
[0] => 'Hello world'
[1] => 'green apples'
[2] => 'red grapes'
)