4

I am using preg_match() to extract pieces of text from a variable, and let's say the variable looks like this:

[htmlcode]This is supposed to be displayed[/htmlcode]

middle text

[htmlcode]This is also supposed to be displayed[/htmlcode]

i want to extract the contents of the [htmlcode]'s and input them into an array. i am doing this by using preg_match().

preg_match('/\[htmlcode\]([^\"]*)\[\/htmlcode\]/ms', $text, $matches);
foreach($matches as $value){
return $value . "<br />";
}

The above code outputs

[htmlcode]This is supposed to be displayed[/htmlcode]middle text[htmlcode]This is also supposed to be displayed[/htmlcode]

instead of

  1. [htmlcode]This is supposed to be displayed[/htmlcode]
  2. [htmlcode]This is also supposed to be displayed[/htmlcode]

and if have offically run out of ideas

kapa
  • 77,694
  • 21
  • 158
  • 175
Simon Andersson
  • 365
  • 3
  • 15

4 Answers4

3

A * grouper is greedy, i.e. it will eat everything until last [/htmlcode]. Try replacing * with non-greedy *?.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
3

As explained already; the * pattern is greedy. Another thing is to use preg_match_all() function. It'll return you a multi-dimension array of matched content.

preg_match_all('#\[htmlcode\]([^\"]*?)\[/htmlcode\]#ms', $text, $matches);
foreach( $matches[1] as $value ) {

And you'll get this: http://codepad.viper-7.com/z2GuSd

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
2

* is by default greedy, ([^\"]*?) (notice the added ?) should make it lazy.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
2

Look at this piece of code:

preg_match('/\[htmlcode\]([^\"]*)\[\/htmlcode\]/ms', $text, $matches);
foreach($matches as $value){
return $value . "<br />";
}

Now, if your pattern works fine and all is ok, you should know:

  • return statement will break all loops and will exit the function.
  • The first element in matches is the whole match, the whole string. In your case $text

So, what you did is returned the first big string and exited the function.

I suggest you can check for desired results:

$matches[1] and $matches[2]

sybear
  • 7,837
  • 1
  • 22
  • 38