Jump to content

One Of My Preg_Replace Lines Does Not Work.


leke

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.