Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eregi_replace 1

Status
Not open for further replies.

ThomasJSmart

Programmer
Joined
Sep 16, 2002
Messages
634
y is it so difficult to find a simple answer to a simple question?

this is what i want:
eregi_replace("<font class=title>*</font>","",$string);

but it dun work :(

the point is to remove all instances of <font class=title>*</font> from $string. where * is supposed to be a wildcard. So <font class=title>bla</font> and
<font class=title><b>big bla</b></font> would be removed.

thanks,


I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
LOad the body of the page into $body variable and try:
Code:
$pattern = '(<font class=title>)(*)(</font>)';
$replacement = '';
$body = eregi_replace($pattern, $replacement, $body);

echo $body;
 
Warning: eregi_replace(): REG_BADRPT

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Try preg_replace instead:

Code:
$pattern = "/<font class=title>*?</font>/i";
$replacement = '';
$body = preg_replace($pattern, $replacement, $body);

echo $body;
 
that should be $pattern = '(<font class=title>)(.*)(</font>)';

not ethe .* thats any character any number of times.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
the preg replace gave :
Warning: Unknown modifier 'f'

and $pattern = '(<font class=title>)(.*)(</font>)';
worked :D cheers!
.* is the key :P



I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
be careful:
Code:
<font class=title><b>big bla</b></font><font>asd</font>

in the above case ur pattern may not work because * is basically greedy, so add this:
/(<font class=title>)(.*)(</font>)/U

the ungreedy operator...


Known is handfull, Unknown is worldfull
 
(<font class=title>)(.*)(</font>)/U isnt removing anything...

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
ie, the regexp is not working at all???

Known is handfull, Unknown is worldfull
 
looking at it, if you have more than one occurence,you will strip everything from the first <font class=.. to the last </font>.

to strip multiple incidences, use this intstead:

Code:
<?php
$body='before the first tag <font class=title>some stuff in the middle of tags 1</font> end of first tags <br> start of second <font class=title>some more stuff in the middle of tags 2</font> end of tags 2<br> rest of page';

$pattern = '/\<font class=title>(.*?)\<\/font>/si';
$replace='';
$body=preg_replace($pattern,$replace,$body);
//echo "<textarea cols=30 rows=5>$body</textarea><br>";// prove it works.
echo $body;
?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
that was the idea, just that i use a /U (ungrredy operator) tag...

Known is handfull, Unknown is worldfull
 
that was the idea, just that i use a /U (ungrredy operator) tag...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top