user3789894
user3789894

Reputation: 23

Php Preg_match for beginners

I need some help with preg_match(). I have this assignment about preg_match() that I need to be done.

I have this string like this "[test|test2|test3] is [test4|test5|test6] " I need to get the string inside the [],

Here is my attempt:

$str = "[test|test2|test3] is [test4|test5|test6] "; 
preg_match_all("/\[(.*)\]/", $str, $output);
print_r($output);

as I read the documentation, I think its right.

My expected output

test|test2|test3

and

test4|test5|test6

but on my print_r();

My actual output:

test|test2|test3] is [test4|test5|test6

Also is it possible that I can explode the strings the way like this?

array(
   [0] => test|test2|test3,
   [1] => is,
   [2] => test4|test5|test6
)

I'm working on a function that would parse the input and output a string like this:

test is test4
test is test5
test is test6

test2 is test4
test2 is test5
test2 is test6

test3 is test4
test3 is test5 
test3 is test6

but also it should work on input that look like this

 the [test|test2|test3] is so [test4|test5|test6]  and [test7|test8|test9] 

or

the unicorn is so [test4|test5|test6]  and [test7|test8|test9] 

Upvotes: 2

Views: 156

Answers (2)

vogomatix
vogomatix

Reputation: 5041

You need to learn about greedy and non-greedy matching. Your match is currently doing greedy matching which means that the code will put the maximum number of characters in a match.

You can state that your matching is non-greedy by use of the ? operator in your regex.

preg_match_all("/\[(.*?)\]/", $str, $output);
#                     ^ non-greedy

There are a number of ways to do this - another alterative would be to capture all characters which are not a ]:

preg_match_all("/\[([^\]*)\]/", $str, $output);

I'm not a fan of the second one because of the increased visual complexity, but as you can tell from the other answer, opinions vary!

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Quantifiers are greedy by default. Your . is matching as much as possible.

Two solutions:

  1. Use (.*?) instead. This makes the quantifier un-greedy, and will match as little as possible.

  2. Use ([^\]]*). This is preferable because it clearly defines your endpoint.

Upvotes: 1

Related Questions