Adam Posted December 26, 2008 Share Posted December 26, 2008 Continuing my last topic which I frst thought was solved (http://www.phpfreaks.com/forums/index.php/topic,231586.0.html) .. I have a new problem, with the expression: \[pre\].+?\[\/pre\] With preg_replace it replaces all occurences of the matched string, but preg_match only seems to match the first? $str = '[tag]This should be replaced with ::tag:: and added to $matches[/tag] .. [tag]So should this![/tag] But this shouldn\'t!'; preg_match('#\[tag\].+?\[\/tag\]#', $str, $matches); $str = preg_replace('#\[tag\].+?\[\/tag\]#', '::tag::', $str); print 'After preg_replace: ' . $str . '<br />Matches from preg_match:<pre>'; print_r($matches); print '</pre><br /><br />'; That will return something like: After preg_replace: ::tag:: .. ::tag:: But this shouldn't! Matches from preg_match: Array ( [0] => [tag]This should be replaced with ::tag:: and added to $matches[/tag] ) I'm stuck ??? Anyone? Cheers! A Quote Link to comment https://forums.phpfreaks.com/topic/138485-solved-further-problems-with-preg_match/ Share on other sites More sharing options...
nrg_alpha Posted December 27, 2008 Share Posted December 27, 2008 but preg_match only seems to match the first? preg_match is only meant to stop after the first pattern match it successfully finds. Try preg_match_all, as this will continue from where it left off after the previous successful match. Quote Link to comment https://forums.phpfreaks.com/topic/138485-solved-further-problems-with-preg_match/#findComment-724223 Share on other sites More sharing options...
Adam Posted December 27, 2008 Author Share Posted December 27, 2008 Hah of course, I even knew that function existed just never thought about it. Though reading the PHP manual, it doesn't make it particularly clear that it only returns the first result. Ah well, sorted now thanks a lot! A Quote Link to comment https://forums.phpfreaks.com/topic/138485-solved-further-problems-with-preg_match/#findComment-724430 Share on other sites More sharing options...
.josh Posted December 27, 2008 Share Posted December 27, 2008 From the manual: Return Values preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred. Dunno how much clearer you expect them to be.... Quote Link to comment https://forums.phpfreaks.com/topic/138485-solved-further-problems-with-preg_match/#findComment-724539 Share on other sites More sharing options...
Adam Posted December 27, 2008 Author Share Posted December 27, 2008 Hah.. dam it, missed that, or read the first sentence wrong. Anywho, topic solved! Cheers for help! A Quote Link to comment https://forums.phpfreaks.com/topic/138485-solved-further-problems-with-preg_match/#findComment-724545 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.