我想知道是否有办法将一串短语分成一个数组。每个短语都用单引号括起来。我很感激任何建议!
我尝试了什么
explode(' ', $string);
输入
$string = "'Hello world' 'green apples' 'red grapes'";
期望输出
//ary[0] = 'Hello World'
//ary[1] = 'green apples'
//ary[2] = 'red grapes'
非常感谢提前!
答案 0 :(得分:2)
更正$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
)
答案 1 :(得分:2)
试试这个:
$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'
)