Jump to content

Preg_match problem


sastro

Recommended Posts

$str='....

<content:encoded>This is the embeded content</content:encoded>

...';

 

I want to get the text "This is the embeded content"

I'm using

preg_match('/<content:encoded>(.*)<\/content:encoded>/i',$str,$match); and

preg_match('/<content\:encoded>(.*)<\/content\:encoded>/i',$str,$match);

and print_r($match); but array is empty

 

Please help

 

Link to comment
https://forums.phpfreaks.com/topic/185145-preg_match-problem/
Share on other sites

In theory your first pattern is correct, it depends on the context in which it is used. Since by default the . match will be greedy you could end up matching more than you want to. If you are trying to say it's not currently working I would guess you need to add the s modifier so that the . matches \n characters.

Link to comment
https://forums.phpfreaks.com/topic/185145-preg_match-problem/#findComment-977329
Share on other sites

@sastro,

 

Just be careful about using .* as depending on the string in question, it might match more than you expected. Typically, it will match as much as it can, then backtrack to the last instance of </content:encoded> found (which may, or may not be desirable).

 

You can read up about the pitfalls of using greedy quantifiers here (post #11 and #14 in particular).

Link to comment
https://forums.phpfreaks.com/topic/185145-preg_match-problem/#findComment-977368
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.