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 Link to comment https://forums.phpfreaks.com/topic/42871-i-just-dont-think-i-understand-it/ 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. Link to comment https://forums.phpfreaks.com/topic/42871-i-just-dont-think-i-understand-it/#findComment-208203 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. Link to comment https://forums.phpfreaks.com/topic/42871-i-just-dont-think-i-understand-it/#findComment-208281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.