Jump to content

[SOLVED] Reverse of ereg_replace


Canman2005

Recommended Posts

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.

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

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"

 

 

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

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. 

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.