I am trying to replace some html in php with preg_replace and I don't seem to get the regex fine, because I always screw it up.
Could you please help me with it ?
I have the following:
<ol><li>text</li><li>text</li></ol>
<ol>&开发者_如何学Golt;li><ul><li>text</li></ul></li></ol>
And I want to insert a <span></span>
right after <li>
and place the text in <p>
. I must note that I don't want the <li>
inside the <ul>
to be affected.
Thank you !
Rather than trying to write a single regexp to handle this (I think it might be possible to do it with a look-behind type match) it'd be easiest to:
- find each
<ol>
withpreg_match_all()
- iterate through each
<li>
in the results ensuring that it's not wrapped in a<ul>
I recommend this http://code.google.com/p/phpquery/
Using a regexp for this is a recipe for bugs.
Parse the html so as to be able to manipulate it as objects. There are plenty of libraries to do so: DOM extension, SimpleXML, phpquery, etc. Then manipulate it as you need.
精彩评论