从一个字符串到另一个字符串获取特定部分


Getting specific parts from a string to another

我有一个字符串变量,如下

$str= '[[["how ","no use","",""],["to ","no use","",""],["solve this","no use","",""]]]';

我想把这个字符串的一些部分做成另一个,就像下面的一样

$result="how to solve this";

是否可以从$str创建$result?

我是php的新手,有人帮我解决这个问题或任何解决这个问题的想法。

由于字符串是json,因此可以使用json_decode将该字符串转换为数组。由于整个东西都包裹在[]中,所以reset用于剪切外部阵列。

$string= '[[["how ","no use","",""],["to ","no use","",""],["solve this","no use","",""]]]';
$result = '';
foreach(reset(json_decode($string)) as $piece)
{
    $result .= reset($piece);
}
echo $result;

给定字符串的结构,您可能会使用preg_grep来查找任何与["匹配的子字符串,直到下一个"

$regex = '/''["([^"])"/';
$matches = preg_grep($regex,$str);
$result = implode('',$matches);

注意。。。我的正则表达式可能有些生疏;-)