Forum Moderators: coopster

Message Too Old, No Replies

how to "eregi" a few lines

         

zozzen

8:57 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Hi,

I'd like to replace a few line of HTML which is in this pattern.
-
<li id="1"><a href></a></li>
<li id="2"><a href></a></li>
<li id="3"><a href></a></li>
<li id="4"><a href></a></li>

By using eregi("<li id=\"1">(.*)</li>", "", $page) ,
i can replace it a line by a line, but it seems very inefficient.

Is there very regular expression that can indicate a pattern like the above?

Thanks for all advices!

eelixduppy

12:46 am on Feb 26, 2008 (gmt 0)



I am not exactly sure what you are looking for here; could you please elaborate? Also, it seems that if you are looking for replacement then eregi isn't the function you are looking for, but rather you should be looking for preg_replace [php.ner].

zozzen

4:25 am on Feb 26, 2008 (gmt 0)

10+ Year Member



sorry i made a mistake. I'm actually writing a code to fetch a webpage, but would like to remove some unnecessary HTML codes.

A part of code I'd like to remove is like this:
<li id="1"><a href="http://www.yahoo.com">Yahoo</a></li>
<li id="2"><a href="http://www.google.com">Google</a></li>
<li id="3"><a href="http://www.facebook.com">Facebook</a></li>
<li id="4"><a href="http://www.gmail.com">Gmail</a></li>

In order to remove them, the PHP code I wrote is like this:

$content = eregi_replace("<li id=\"1\">(.*)</li>", "", $content); $content = eregi_replace("<li id=\"2\">(.*)</li>", "", $content);
$content = eregi_replace("<li id=\"3\">(.*)</li>", "", $content);
$content = eregi_replace("<li id=\"4\">(.*)</li>", "", $content);

Although the above PHP code can do exactly what I want, but it seems that the code is too long.

As the content between <li>....</li> changes all the time, it's impossible to use the following code to shorten the removal process:
$content = eregi_replace("<li id=\"1\"><a href=\"http://www.yahoo.com\">Yahoo</a></li>(.*)<li id=\"4\"><a href=\"http://www.gmail.com\">Gmail</a></li>", "", $content);

What would you do to replace this?
<li id=1> .....
...............
..............
...........</li>

Any hints will be greatly appreciated.

eelixduppy

4:38 am on Feb 26, 2008 (gmt 0)



Now you just have to deal with the number in the pattern. Try a pattern like the following:

$pattern = "<li id=\"[0-9]+\">(.*)</li>";

zozzen

5:07 am on Feb 26, 2008 (gmt 0)

10+ Year Member



Thanks very much!

By using "<li id=\"[0-9]+\">(.*)</li>" , i still have to use 4 lines of eregi_replace () to replace the unnecessary. Is there any regular expression that can express the following?

<li id=1> .........</li>
..................</li>
..................</li>
..................</li>

d40sithui

4:20 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



Try this:


<?
$content="
<li id=\"1\"><a href=\"http://www.yahoo.com\">Yahoo</a></li>
<li id=\"2\"><a href=\"http://www.google.com\">Google</a></li>
<li id=\"3\"><a href=\"http://www.facebook.com\">Facebook</a></li>
<li id=\"4\"><a href=\"http://www.gmail.com\">Gmail</a></li>
<li><a href=\"www.webmastworld.com\">webmasterworld</a></li>";
echo "\$content before:<br> $content<br>";
$pattern = "/\<li\sid=\"[0-9]+\"\>(.*)\<\/li\>/";
$replacement ="replaced";
$content = preg_replace($pattern, $replacement, $content);
echo "\$content after:<br> $content";
?>

eelixduppy

5:21 pm on Feb 26, 2008 (gmt 0)



Just curious, but these list elements are contained within an (un)organized list somewhere on the page. Does this list have an id that we could use to distinguish it from anything else in the HTML? This way we could just get rid of the whole list.