leke Posted November 22, 2012 Share Posted November 22, 2012 One of my preg_replace lines doesn't work and I can't figure out why. It's very similar to another line which works fine. $quote = '<ol> <li> <span class="bold quote_actor"> Delia Surridge: </span> <span class="line"> Is it meaningless to apologize? </span> </li> <li> <span class="bold quote_actor"> V: </span> <span class="line"> Never. </span> </li> <li> <span class="bold quote_actor"> Delia Surridge: </span> <span class="line"> I'm so sorry. </span> </li> </ol>'; $quote = preg_replace("'<ol>(.*?)<li>'", '', $quote); // first 2 tags $quote = preg_replace("'\s+'", ' ', $quote); // more than one space $quote = preg_replace("'</li>(.*?)<li>'", '<br /><br />', $quote); // separate quote lines $quote = preg_replace("'</li>(.*?)</ol>'", "\n", $quote); // last 2 tags echo $quote; So, everything works fine except $quote = preg_replace("'<ol>(.*?)<li>'", '', $quote); // first 2 tags ...which doesn't do its job. How come? It's very similar to $quote = preg_replace("'</li>(.*?)</ol>'", "\n", $quote); // last 2 tags Which works just fine. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/271028-one-of-my-preg_replace-lines-does-not-work/ Share on other sites More sharing options...
DavidAM Posted November 25, 2012 Share Posted November 25, 2012 The "." (dot) will not match a new-line unless you use the "s" modifier on the Regular Exp<b></b>ression. $quote = preg_replace("'<ol>(.*?)<li>'s", '', $quote); // first 2 tags Quote Link to comment https://forums.phpfreaks.com/topic/271028-one-of-my-preg_replace-lines-does-not-work/#findComment-1394875 Share on other sites More sharing options...
.josh Posted November 27, 2012 Share Posted November 27, 2012 Sidenote: quotes are not a very good pattern delimiter to use. They come up often in patterns so it causes extra unecessary escaping, used as string delimiters so may cause issues with writing the pattern correctly to begin with, etc.. so as a best practice, you should use an uncommonly used symbol for your pattern delimiter. I personally favor the tilde ~ because it has no special meaning for anything and stands out quite nicely. $quote = preg_replace("~<ol>(.*?)<li>~s", '', $quote); // first 2 tags Quote Link to comment https://forums.phpfreaks.com/topic/271028-one-of-my-preg_replace-lines-does-not-work/#findComment-1395649 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.