Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Remove Image with PHP



10:46 pm on Feb 1, 2009 (gmt 0)

5+ Year Member

Hey, I am parsing a rss feed from yahoo (rss.news.yahoo.com/rss/politics) and am trying to strip out the image tags from the feed. I am awful at regexp but I have been trying various expressions and preg_replace to remove them.

/<img[^>]* src=\"([^\"]*)\"[^>]*>/i
/(\<img src=\"\w{0,}\"( ?(\w{0,}=\"\w{0,}\") ?)*\>)/

these are some of the expressions i have been using and the actually code I am using is

preg_replace($regExp, "", $string);

[edited by: eelixduppy at 10:37 pm (utc) on Feb. 2, 2009]
[edit reason] disabled smileys [/edit]


10:26 pm on Feb 2, 2009 (gmt 0)

10+ Year Member

Not sure why, but when I look at the source of that RSS feed I see all the tag opening brackets actually do not use "<", but rahter "&#60;", so your PREGs should actually look like that:

Hope that helps!


5:03 am on Feb 3, 2009 (gmt 0)

5+ Year Member

I checked that but it doesn't seem to help, I thought that was strange as well.


1:10 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Try for both, either the html entity for the less than sign, or the sign itself.
$pattern = '/(&#60;<)img[^>]+>/'; 
$subject = preg_replace($pattern, '', $subject);

Don't forget that the forum breaks the pipe character, you need to retype that if you copy/paste from here.


8:23 pm on Feb 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

@coopster: your pattern won't work if the image tag is xhtml i.e ending on /> I tried same regex on my xhtml where I fetch latest blog posts from wordpress blog and display on the main site homepage but it was failing so I modified it to the following

$pattern = '/(&#60;<)img[^>]+\/>/';

and it worked.

[edited by: eelixduppy at 8:58 pm (utc) on Feb. 17, 2009]
[edit reason] disabled smileys [/edit]


12:06 am on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The pattern should work for either HTML or XHTML, without your modification. The "one or more of anything that is not the greater than sign"
includes the slash.


12:39 am on Feb 18, 2009 (gmt 0)

5+ Year Member

if you can't figure out what that first character is, why don't you try this:
$pattern = '/.img[^>]+>/i';

and if you wanna figure out what it is, you can try:
$pattern = '/(.)img[^>]+>/i';
and preg_match it with a backreference to that group.. just an idea.


Featured Threads

Hot Threads This Week

Hot Threads This Month