sastro Posted December 14, 2009 Share Posted December 14, 2009 $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 Quote Link to comment https://forums.phpfreaks.com/topic/185145-preg_match-problem/ Share on other sites More sharing options...
cags Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185145-preg_match-problem/#findComment-977329 Share on other sites More sharing options...
sastro Posted December 14, 2009 Author Share Posted December 14, 2009 It works with s modifier. Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/185145-preg_match-problem/#findComment-977330 Share on other sites More sharing options...
nrg_alpha Posted December 14, 2009 Share Posted December 14, 2009 @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). Quote Link to comment https://forums.phpfreaks.com/topic/185145-preg_match-problem/#findComment-977368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.