67

Hey, I need to delete all images from a string and I just can't find the right way to do it.

Here is what I tryed, but it doesn't work:

preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;

Any ideas?

2
  • 3
    $content = preg_replace(..., $content); Commented Jul 10, 2009 at 1:08
  • you can use txt2re.com to formulate regular expression. Usually, I use this one and after that, modify the code as per my need. Commented Feb 4, 2019 at 9:03

7 Answers 7

177

Try dropping the \ in front of the >.

Edit: I just tested your regex and it works fine. This is what I used:

<?
    $content = "this is something with an <img src=\"test.png\"/> in it.";
    $content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 
    echo $content;
?>

The result is:

this is something with an (image)  in it.
Sign up to request clarification or add additional context in comments.

2 Comments

Can you make this except of (image) it will replace with the alt of an image?
Try dropping the \ in front of the >. preg_replace("/<img[^>]+\>/i", "(image) ", $content); Hows that's different from the actual question?
23

You need to assign the result back to $content as preg_replace does not modify the original string.

$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);

Comments

11

I would suggest using the strip_tags method.

1 Comment

This strips all tags, not only img tags.
8

Sean it works fine i've just used this code

$content = preg_replace("/<img[^>]+\>/i", " ", $content); 
echo $content;

//the result it's only the plain text. It works!!!

Comments

1

I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!

$news = $row_latest_news ['content'];
$news = preg_replace("/<img[^>]+\>/i", "", $news); 
if (strlen($news) > 300){
echo substr($news, 0, strpos($news,' ',300)).'...';
} 
else { 
echo $news; 
}

1 Comment

Comments use mini-Markdown formatting: link italic bold code. The post author will always be notified of your comment. To also notify a previous commenter, mention their user name:
-2
$this->load->helper('security');
$h=mysql_real_escape_string(strip_image_tags($comment));

If user inputs

<img src="#">

In the database table just insert character this #

Works for me

Comments

-4

simply use the form_validation class of codeigniter:

strip_image_tags($str).

$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');

1 Comment

Did he say, he is using CodeIgniter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.