homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Remove Image with PHP

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

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)

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)

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)

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)

@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)

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)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved