Canman2005 Posted July 12, 2009 Share Posted July 12, 2009 Hi all I'm doing the following ereg_replace("<img[^>]*>", "", $string); which is stipping out all images from my string. Is there a way to reverse this, so it strips out everything but the image? Thanks Quote Link to comment Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 if you want to keep it as a string, you can do $string = preg_replace('~.*?(<img[^>]*>).*~is','$1',$string); or if you want to just retrieve all image tags and have them in an array, you can do preg_match_all('~<img[^>]*>~i',$string,$images); // image tags stored in $images array and on that note, you should be using the pcre functions (preg_xxx) instead of the posix functions (ereg_xxx) as the posix functions are deprecated. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted July 12, 2009 Author Share Posted July 12, 2009 Cool cool preg_replace('~.*?(<img[^>]*>).*~is','$1',$string); seemed to work fine Is there a way to detect if an image appears in the $string and if no image exists, then it wouldnt run preg_replace('~.*?(<img[^>]*>).*~is','$1',$string); Thanks Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted July 12, 2009 Author Share Posted July 12, 2009 Really I just want to strip out the ALT tag that's defined on each image i;m displaying Is there a way to strip out ALT tags assigned to images? Quote Link to comment Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 be more specific about "then it wouldn't run." preg_replace only replaces what it finds. So if your string doesn't have any image tags, nothing will get replaced. Example: $string = "something something"; $string = preg_replace('~.*?(<img[^>]*>).*~is','$1',$string); echo $string; // output: something something $string = "something <img src='...'> something"; $string = preg_replace('~.*?(<img[^>]*>).*~is','$1',$string); echo $string; // output: <img src='...'> Really I just want to strip out the ALT tag that's defined on each image i;m displaying Is there a way to strip out ALT tags assigned to images? Well that's something entirely different. Why didn't you mention that in the first place? And be more specific. Are you saying you have a document and if there are image tags in it, you want to check to see if there is an alt tag in the image and remove it? So for instance, "something <img src='...' alt='...'> something" to "something <img src='...'> something" Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted July 12, 2009 Author Share Posted July 12, 2009 Sorry Let me explain what im doing. Im displaying an RSS feed and with each RSS article is an image, so what i'm trying to do is strip out all the article text and just display its image. The problem is that some articles have an image and some dont. If I use the suggested solutions, everything works fine and images are displayed without the article text. The problem is that when it comes to an article that has not got an image, then it seems to display the text inside the ALT tag. Does that make much sense? Thanks very much so far everyone Quote Link to comment Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 There's no such thing as an ALT tag. 'alt' is an attribute inside an image tag, so the article would not have an ALT attribute unless there was an image tag (nor would the preg_replace be replacing anything unless there was an image tag). Sounds like to me the image src link is broken, so the alt text is being displayed instead of the image. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted July 12, 2009 Author Share Posted July 12, 2009 The problem im finding is this If I use the following code like suggested $img = 'text start <img src="http://www.google.co.uk/intl/en_uk/images/logo.gif"/> text end'; $image = preg_replace('~.*?(<img[^>]*>).*~is','$1',$img); Then it works fine and displays the image and hides the text. But if no image is available in the string such as $img = 'text start text end'; $image = preg_replace('~.*?(<img[^>]*>).*~is','$1',$img); Then it prints the text, whereas I need to get it to not display the text, even if no image exists. Any ideas? Thanks very much Quote Link to comment Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 Okay so use the preg_match_all then. preg_match_all('~<img[^>]*>~i',$string,$images); $string = implode($images[0]); If there is no image(s) to be found, it will just be an empty string. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted July 12, 2009 Author Share Posted July 12, 2009 Thank you sir Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.