Forum Moderators: coopster

Message Too Old, No Replies

Regex: how to remove the first tag?

         

zozzen

9:15 am on May 12, 2008 (gmt 0)

10+ Year Member



Hello, i need to use preg_replace/ eregi_replace to retrive and trim some codes of a page like this


<script>.....</script>
<title>....</title>
<h2 class=a>
// Trim all the lines above , and keep below.
<h2 class=a>
<h2 class=a>
<h2 class=a>
<h2 class=a>
<h2 class=a>

My code is:
$output = eregi_replace("(.*)<h2 class=a>", "", $output);

When it runs, it trims all pages until the last <h2 class=a>, instead of the first <h2 class=a>.

Can anyone tell me how to locate the first <h2 class=a> in the page?

Thanks a lot!

FourDegreez

1:44 pm on May 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_replace offers an optional limit argument, which you can set to 1.

mehh

7:40 pm on May 12, 2008 (gmt 0)

10+ Year Member



$output = eregi_replace("(.*?)<h2 class=a>", "", $output);

The ? makes the match non greedy so it takes as little as possible rather than the oposite

zozzen

8:13 pm on May 14, 2008 (gmt 0)

10+ Year Member



a small mistake. It should be (.*) instead of (.*?).
trying to set a limit argument now, thanks a lot!