-3

我想知道是否有办法将一串短语拆分为一个数组。每个短语都用单引号括起来。我很感激任何建议!

我试过的

 explode(' ', $string);

输入

$string = "'Hello world' 'green apples' 'red grapes'";

期望的输出

  //ary[0] = 'Hello World'
  //ary[1] = 'green apples'
  //ary[2] = 'red grapes'

提前谢谢了!

4

2 回答 2

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
)
于 2013-07-24T14:20:41.943 回答
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'
)
于 2013-07-24T14:24:57.687 回答