Php 用“查找所有字符”;[string]";并将其转换为一个数据数组
我有一些这样的文本值Php 用“查找所有字符”;[string]";并将其转换为一个数据数组,php,regex,string,Php,Regex,String,我有一些这样的文本值 $str = 'A quick [color] [animal] [action] over the lazy dog.'; 我想找到带有“[”和“]”的所有数据,因此如果我有一条长文本,我只需执行[电话号码]或其他操作。我可以将其转换为数组值吗 我想要的输出: $array = ('[color]','[animal]','[action]'); 因为我稍后将在str\u replace()中使用此选项。有什么办法可以这样做吗 感谢您的帮助您想使用preg\u mat
$str = 'A quick [color] [animal] [action] over the lazy dog.';
我想找到带有“[”和“]”的所有数据,因此如果我有一条长文本,我只需执行[电话号码]
或其他操作。我可以将其转换为数组值吗
我想要的输出:
$array = ('[color]','[animal]','[action]');
因为我稍后将在str\u replace()
中使用此选项。有什么办法可以这样做吗
感谢您的帮助您想使用
preg\u match\u all
:
$str = "A quick [color] [animal] [action] over the lazy dog.";
preg_match_all("/\[[^\]]*\]/", $str, $matches);
echo json_encode($matches); // [["[color]","[animal]","[action]"]]
您想使用
preg\u match\u all
:
$str = "A quick [color] [animal] [action] over the lazy dog.";
preg_match_all("/\[[^\]]*\]/", $str, $matches);
echo json_encode($matches); // [["[color]","[animal]","[action]"]]
这确实有效!谢谢如果我使用
\u this
而不是[this]
,会怎么样?在这种情况下,使用preg\u match\u all(“/\\\[^\]*\\\\\/”,$text,$match)代码>为什么我也要在其中获取\数据?这实际上是可行的!谢谢如果我使用\u this
而不是[this]
,会怎么样?在这种情况下,使用preg\u match\u all(“/\\\[^\]*\\\\\/”,$text,$match)代码>为什么我也要获取其中包含\的数据?