1

im really stuck in my code.

I have a text thats looks like this

[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality Test) [[QVQ]] New Car

And I need the text behind the [[QVQ]]. I read about preg match.

preg_match_all('/\[[QVQ\]](.*?)\[[QVQ\]]/s', $input, $matches);

But how do I use preg_match in this case to get the matches into single Variables?

Like $match1 , $match2 ,$match3 [...]

chris85
  • 23,846
  • 7
  • 34
  • 51

2 Answers2

0

Every [ and ] needs the be escaped, without escaping those you make a character class which is a list of allowed characters, or a range (0-9, would be any single number). You can read more here, http://www.regular-expressions.info/charclass.html.

Code:

preg_match_all('/\[\[QVQ\]\](.*?)\[\[QVQ\]\]/s', '[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality 
Test) [[QVQ]] New Car', $matches);
print_r($matches[1]);

Demo: https://eval.in/826134
Regex Demo: https://regex101.com/r/nHELMW/1/

$matches[1] will be an array of all found terms.

Update:

Since what you actually have is the [[QVQ]] as a marker you should just use preg_split.

$matches = preg_split('/\[\[QVQ\]\]/', '[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality Test) [[QVQ]] New Car', -1, PREG_SPLIT_NO_EMPTY);

$matches will then be an array with all your matches.

Demo: https://eval.in/826151

chris85
  • 23,846
  • 7
  • 34
  • 51
  • Thank you very much! But "New Zealand | WATER [ 2nd Test ]" is missing in this example :( – user8245711 Jul 03 '17 at 00:28
  • [[QVQ]] Is starting delimiter, and a new [[QVQ]] is an other starting delimiter so its also an ending deliminter, Basicly I want the texts behind each of the starting delimiter's – user8245711 Jul 03 '17 at 00:36
  • Both seem working I think but I struggle to find a way too make it work with $matches[1] like you did before, so I can use the variable again – user8245711 Jul 03 '17 at 00:46
0

Because chris isn't using look arounds with his preg_match_all() pattern, not all substrings are captured.

This will capture all of the substrings (without any unwanted leading/trailing white space characters) using preg_match_all(): /(?<=\]{2}) ?\K.+?(?=$| \[{2})/ Pattern Demo

var_export(preg_match_all('/(?<=\]{2}) ?\K.+?(?=$| \[{2})/', $input, $out) ? $out[0] : []);

chris' preg_split() method does not contend with leading and trailing spaces. This is a corrected/refined method (this is my recommended regex method):

var_export(preg_split('/ ?\[{2}QVQ\]{2} ?/', $input, 0, PREG_SPLIT_NO_EMPTY));

This is a non-regex method:

var_export(array_values(array_filter(array_map('trim', explode('[[QVQ]]', $input)), 'strlen')));

Breakdown of non-regex method:

array_values(                          // reindex the array
    array_filter(                      // unset any empty elements 
        array_map('trim',              // remove leading/trailing spaces from each element
            explode('[[QVQ]]', $input) // split on known delimiter
        ),
        'strlen'
     )
 )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136