3

I would like to match all the patterns like [[WHATEVER]]

The text inside those two brackets can be uppercase or lowercase, it can start with space or end with space and can be made of two or more words separated by space

$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]]") 

2 Answers 2

2

You can match them like this:

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

Output of a sample program:

Array                                                                                                                                                                                                                                                  
(                                                                                                                                                                                                                                                      
    [0] => [[QUICK]]                                                                                                                                                                                                                                   
    [1] => [[FOX]]                                                                                                                                                                                                                                     
    [2] => [[whatever]]                                                                                                                                                                                                                                
)    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for this "faster than light" answer ;)
\[\[[^]]*]] is enough.
0
\[\[[^\]]*\]\]

This should do it.

See demo

$re = "/\\[\\[[^\\]]*\\]\\]/im";
$str = "The [[QUICK]] brown [[FOX]] jumped over [[whatever]]";

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.