Forum Moderators: coopster

Message Too Old, No Replies

preg_replace

replace image

         

ikbenhet1

3:26 pm on Jun 5, 2003 (gmt 0)

10+ Year Member



How to replace "<img src='http://someurl.gif'>" to nothing with preg_replace

so far i got:
preg_replace("/(<img)(.*?)(>+)/i","",$string);

but it's doesn't work. can anybody help please?

brotherhood of LAN

3:35 pm on Jun 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hopefully this works, I havent tried it.
preg_replace("/<img [^>]+>/i","",$string);

[^>] just means, keep matching until you find the ">" character, which should be the end of the tag.

//added
you shouldnt really need the parenthesis either...

ikbenhet1

3:50 pm on Jun 5, 2003 (gmt 0)

10+ Year Member



Thanks for your reply, i've tried it, it don't seem to work.

to keep it simple i tried the following:


<?php
$string="... <img src='meis'> .....";
preg_replace("/<img [^>]+>/i","",$string);
echo $string;
?>

the image is still in the string, any input would very much be appriciated.

brotherhood of LAN

4:10 pm on Jun 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you do the preg_replace, you'll need to reassign the variable "string" as the result, i.e.

<?php
$string="... <img src='meis'> .....";
echo $string = preg_replace("/<img[^>]+>/i","",$string);
?>

ikbenhet1

4:27 pm on Jun 5, 2003 (gmt 0)

10+ Year Member




Thanks Brotherhood_of_LAN, that works well.

I am way too optimistic trying to build a search engine, while i'm still struggeling with basic php syntaxes.