Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中解析所有匹配模式的字符串_Php_Arrays_Regex_String_Parsing - Fatal编程技术网

在php中解析所有匹配模式的字符串

在php中解析所有匹配模式的字符串,php,arrays,regex,string,parsing,Php,Arrays,Regex,String,Parsing,我想匹配所有模式,如[[任何]] 这两个括号内的文本可以是大写或小写,可以以空格开头,也可以以空格结尾,可以由两个或多个用空格分隔的单词组成 $string = "The [[QUICK]] brown [[FOX]] jumped over [[whatever]]"; parse $string; //I would like an result to be like array array( 0 => "[[QUICK]]", 1 => "[[FOX]]", 2 =>

我想匹配所有模式,如
[[任何]]

这两个括号内的文本可以是大写或小写,可以以空格开头,也可以以空格结尾,可以由两个或多个用空格分隔的单词组成

$string = "The [[QUICK]] brown [[FOX]] jumped over [[whatever]]";

parse $string; //I would like an result to be like array

array( 0 => "[[QUICK]]", 1 => "[[FOX]]", 2 => "[[whatever]]") 

您可以这样匹配它们:

  $re = "/\\[{2}.*?\\]{2}/"; 
  $str = "The [[QUICK]] brown [[FOX]] jumped over [[whatever]]"; 
  preg_match_all($re, $str, $matches);
  print_r($matches[0]);
a的输出:

这应该可以做到


非常感谢您给出了这个“比光还快”的答案;)<代码>\[\[^]]*]就足够了。
Array                                                                                                                                                                                                                                                  
(                                                                                                                                                                                                                                                      
    [0] => [[QUICK]]                                                                                                                                                                                                                                   
    [1] => [[FOX]]                                                                                                                                                                                                                                     
    [2] => [[whatever]]                                                                                                                                                                                                                                
)    
\[\[[^\]]*\]\]
$re = "/\\[\\[[^\\]]*\\]\\]/im";
$str = "The [[QUICK]] brown [[FOX]] jumped over [[whatever]]";

preg_match_all($re, $str, $matches);