Jump to content

Archived

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

ale1981

remove every image tag in a string? ==SOLVED==

Recommended Posts

This is probably very simple, but I havent got a clue where to start when it comes to preg_replace! What I need is to completely remove any <img> tags from a string and everything in between the tag.

Share this post


Link to post
Share on other sites
strip_tags() to remove all html tags.

[code]<?php

$string = preg_replace('/<(\s*)img[^<>]*>/i', '', $string);

?>[/code]to remove just img

Share this post


Link to post
Share on other sites
I think I might as well ask this now.  All the str replaces I've seen for filtering out HTML / XML tags have these huge lumps of characters, but I don't know /<(\s*)img[^<>]*>/i  works.  Can you give a slight explanation?

Share this post


Link to post
Share on other sites
[b]Jenk:[/b] A few nitpicks. Don't use[tt] ( ) [/tt]if you're not going to use what is captured; use[tt] (?: ) [/tt]instead. White space does not need to be searched for before "img", this is invalid HTML.

[quote author=Wintergreen link=topic=107754.msg433470#msg433470 date=1158159232]
Can you give a slight explanation?
[/quote]

[code]
/
<img ### Match this literally
[^>]*  ### Match any character 0 or more times that is not >
> ### Match this literally
/xi
[/code]


The[tt] /x [/tt]and[tt] /i [/tt] modifiers are explained [url=http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php]here[/url].

Share this post


Link to post
Share on other sites

×

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.