1

I have this string:

<li id="1">3431233</li>
<li id="2">fsdfdsfsdfdsfdsf</li>
<li id="3">abc</li>
<li id="4"> fdsf2342343</li>
<li id="5">123</li>

I want to replace only those lines containing abc or 123 with nothing (3rd and 5th in this case). My problem is that this regex wont work:

^<li>abc<\/li>

It replaces all lis. How can I only limit the replaceing to one certain line? The li differ from line to line and thats dynamic, I do not know how they will look (what classes and ids they might have.)

Thanks!

user1856596
  • 7,027
  • 10
  • 41
  • 63
  • @Charlie: With "nothing". – elclanrs May 06 '13 at 07:31
  • Whats the pattern for the lines that you want removed? Is it the length of 3 chars? – Imperative May 06 '13 at 07:32
  • See the Accepted Answer in the dupe. Instead of loadXML() and saveXML() you use loadHTML() and saveHTML() respectively. Also see http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml/3577662#3577662 and http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element/3820783#3820783 for more examples. – Gordon May 06 '13 at 07:47

3 Answers3

3

Try

^<li>(abc|123|)<\/li>

I tested it here and it worked

HennyH
  • 7,794
  • 2
  • 29
  • 39
2

simply use str_replace

<?php
  str_replace("<li>abc</li>"," ", $yourstring);
  str_replace("<li>123</li>"," ", $yourstring);

EDIT : try this:

    '/<li(.*)>(abc|123)<\/li>/';
egig
  • 4,370
  • 5
  • 29
  • 50