psymastr Posted March 15, 2007 Share Posted March 15, 2007 Hello. Im trying to teach myself Regex stuff, and Ive come to a roadblock. I really dont know what is wrong with this code. I want it to parse <li> TEXT <br /> into <li> TEXT </li> but I seem to have the syntax of the regex part wrong. Whenever I run the code, both my echo statements produce the original $test string. Can someone explain to me what the problem is? <?php $test = "<li>Somestuff<br />"; echo $test . "\n"; $test = preg_replace('/(\<li\>)(.+)(\<\/br \/\>)/', "<li>\\2</li>\n", $test); echo $test; ?> Thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted March 15, 2007 Share Posted March 15, 2007 The red part will not match: <\/br \/\> A cleaner regex would be: %<li>(.+?)< br />% (make sure you remove the space before "br") The % delimiters prevent you from escaping the /, and the ungreedy quantifier (.+?) is required if your string contains more than one li/br pair. Quote Link to comment Share on other sites More sharing options...
psymastr Posted March 15, 2007 Author Share Posted March 15, 2007 Thank you so much I think I understand it a lot better now too. Quote Link to comment 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.