propellerhead Posted March 20, 2009 Share Posted March 20, 2009 Hi. I'm new with regular expressions so this may not be a big deal for you, but I'm stuck for hours on this bit and I can't find the solution. Here I go: I have a string of this kind <custom> probe1 <custom> probe2 </custom> probe3 </custom> <custom> probe4 </custom> <custom> probe5 </custom> I'm trying to match only those custom tags that don't have child (custom) tags in them, so I use this regex /<custom>((?!.*<custom>).*)<\/custom>/s and so, it looks fine on first sight, but there is a problem. When I make the match using preg_match_all it onlly returns the last matching pattern <custom> probe5 </custom> here is the array dump using print_r Array ( [0] => Array ( [0] => <custom> probe5 </custom> [1] => probe5 ) ) How do I fix this? I know that there must be something wrong with the regex, but my knowledge in this field is limited and I can't find the solution. Here is the whole code <?php $string = " <custom> probe1 <custom> probe2 </custom> probe3 </custom> <custom> probe4 </custom> <custom> probe5 </custom>"; $regex = "/<custom>((?!.*<custom>).*)<\/custom>/s"; preg_match_all($regex, $string, $arry, PREG_SET_ORDER); print_r($arry); ?> I'd be very very thankful Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/ Share on other sites More sharing options...
sasa Posted March 20, 2009 Share Posted March 20, 2009 try <?php $string = " <custom> probe1 <custom> probe2 </custom> probe3 </custom> <custom> probe4 </custom> <custom> probe5 </custom>"; //$regex = "/<custom>((?!.*<custom>).*)<\/custom>/s"; //preg_match_all($regex, $string, $arry, PREG_SET_ORDER); $a = explode('<custom>',$string); foreach ($a as $b){ $b = explode('</custom>',$b); if (count($b) > 1) $arry[] = $b[0]; } print_r($arry); ?>f/code] Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/#findComment-789107 Share on other sites More sharing options...
propellerhead Posted March 20, 2009 Author Share Posted March 20, 2009 explode won't work because the <custom> tag is just for an example. The nature of the application requires it to be parsed using regular expression because the attributes that will be placed inside the custom tag <custom attr1="blah" attr2="blah">, and the large content. Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/#findComment-789302 Share on other sites More sharing options...
sasa Posted March 20, 2009 Share Posted March 20, 2009 is some nested tags inside "<custom>" tags Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/#findComment-789412 Share on other sites More sharing options...
propellerhead Posted March 20, 2009 Author Share Posted March 20, 2009 Well yes, since the custom tags will be used in a xhtml environment. Is there a way how to extract all of the custom blocks, not just the last? Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/#findComment-789515 Share on other sites More sharing options...
effigy Posted March 20, 2009 Share Posted March 20, 2009 Regular expressions are not the best tool for nested data, but this works: <pre> <?php $data = <<<DATA <custom> probe1 <custom> probe2 </custom> probe3 </custom> <custom> probe4 </custom> <custom> probe5 </custom> DATA; preg_match_all('%<custom>(??!<custom>)(?!</custom>).)*<\/custom>%s', $data, $matches); print_r($matches); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/#findComment-789545 Share on other sites More sharing options...
propellerhead Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks a lot, that solved my problem. Quote Link to comment https://forums.phpfreaks.com/topic/150250-solved-preg_match_all-returns-only-one-match/#findComment-789552 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.