user4729289
user4729289

Reputation:

parsing string for all matching patterns in php

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

Upvotes: 3

Views: 204

Answers (2)

vks
vks

Reputation: 67968

\[\[[^\]]*\]\]

This should do it.

See demo

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

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

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

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

Upvotes: 2

Related Questions