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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.