php - preg_match acting very strange -


i using preg_match() extract pieces of text variable, , let's variable looks this:

[htmlcode]this supposed displayed[/htmlcode]  middle text  [htmlcode]this supposed displayed[/htmlcode] 

i want extract contents of [htmlcode]'s , input them array. doing using preg_match().

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

the above code outputs

[htmlcode]this supposed displayed[/htmlcode]middle text[htmlcode]this supposed displayed[/htmlcode] 

instead of

  1. [htmlcode]this supposed displayed[/htmlcode]
  2. [htmlcode]this supposed displayed[/htmlcode]

and if have offically run out of ideas

as explained already; * pattern greedy. thing use preg_match_all() function. it'll return multi-dimension array of matched content.

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

and you'll this: http://codepad.viper-7.com/z2gusd


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -