PHP - Remove <Img> Tag from String

PHP - remove img tag from string

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.

How to remove all img tag in php variable?

Using regex you can do it. Php preg_replace() can replace specific text with another. You can use it. The code replace all img tag with empty.

$text = preg_replace("/<img[^>]+>/", "", $text);

See result in demo

Preg_replace wont remove each img tag with src address

You should not use a regex for this. You can use your strpos as you were but move it inside the DOM parsing and compare each img. You can then use removeChild() to remove the appropriate images. (This is an adapted answer from How to delete element with DOMDocument?)

<?php
$inbox_message = '<p> Keep This</p><img src="http://example.com/someimage1.jpeg"><img src="http://example.com/someimage2.jpeg"><img src="http://example.com/someimage3.jpeg"><img src="http://example.com/someimage4.jpeg"><h1>Fake element</h1><img style="OVERFLOW: hidden; WIDTH: 0px; MAX-HEIGHT: 0px" alt="Sample Image" src="http://test.mydomain.com/project433q325/track/Images/signature.gif?id=446&etc=1586624376">';
$doc = new DOMDocument();
$doc->loadHTML($inbox_message);
$imgs = $doc->getElementsByTagName('img');
for($i = $imgs->length; --$i >= 0;){
$node = $imgs->item($i);
if (strpos($node->getAttribute('src'), 'http://test.mydomain.com/project433q325/track/Images/signature.gif?') !== false) {
$node->parentNode->removeChild($node);
}
}
echo $doc->savehtml();

https://3v4l.org/qinLR

You also could use strtolower if $node->getAttribute('src') might contain varying case. The needle for strpos should also be lowercased in that case.

For regex issues...

preg_replace("\<img src\=\"(.+)\"(.+)\/\>/i", '', $src);

The start of the regex is attempting to use a backslash which is not a valid delimiter. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. The starting delimiter must match the ending delimiter. Additionally your $src only contained the value of the attribute so <img src... would never match.

If you were to get that functioning the .+ would need to be replaced with the URI you wanted to check against.

BUT regex really is the wrong approach here. Use a parser, as you were, for these type of jobs. Regex shouldn't be used for structured data. If it is structured there likely already are functions written for it.

Remove img tag containing a word from Html string using PHP

You could try this:

<?php
$var = "abc<img src='http://website/Tracker.php?var1=5&var2=8'>def";
echo 'Before replace: <textarea style="width:800px;">' . $var . '</textarea><br/>';
$var = preg_replace('~<img[^>]{1,}Tracker.php[^>]{1,}>~', '', $var);
echo 'After replace: <textarea style="width:800px;">' . $var . '</textarea><br/>';
?>

I hope this helps!

Remove IMG tag from String for a specific image with PHP

Something like:

$str = 'Lorem <img alt="Sample Image" class="" src="myimage.png"/> ipsum <img class="" alt="Sample Image" src="myimage.png"/> dolor <img src="myimage.png"/> sit...';
echo preg_replace('!<img.*?src="myimage.png".*?/>!i', '', $str);
// output: "Lorem ipsum dolor sit..."

maybe?

Remove 'img' tag from string using preg_replace

If you are not forced to use regular expressions to parse HTML content, then I'd suggest using PHP's strip_tags() function along with its allowed_tags argument.

This will tell PHP to remove all the html tags and leave the ones your specified in the allowed_tags.

Example usage taken from the documentation -

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
// output : Test paragraph. Other text

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
// output - <p>Test paragraph.</p> <a href="#fragment">Other text</a>

So, if you simply specify all the HTML tags in allow_tags except the <img> tag, you should get the results that you need - only removing the <img> tag from your string.

how to remove whole img element with a SRC value in an UTF-8 string?

Use preg_replace directly, no condition and preg_match needed.

$text = 'some text1 <img src="1.jpg" />  some text2 <img src="2.jpg">';

echo preg_replace('~(<img.*[\'"]1\.jpg[\'"].*>)~', '', $text); // some text1 some text2 <img src="2.jpg">

The first ['"] is needed to avoid removing eg. a1.jpg file, the second one is optional. It's better to understand how it works.

UPDATE
Due to the comment below, here is updated version with a variable name:

$file = '1.jpg';
$text = 'some text1 <img src="1.jpg" /> some text2 <img src="2.jpg" />';
echo preg_replace('~<img[^>]+[\'"]' . $file . '[\'"].*?/>~', '', $text);

How to Remove a specific Img tag from string

Hoping that the html source is a well-formatted html/xml/[whatever markup language], you can remove bad tags by using XmlDocument to read your source then remove bad elements detect them by “alt” attribute.
A little code demonstration:

Function ClearBadImgTags(source As String) As String
Dim xDoc As XmlDocument = New XmlDocument
Try

xDoc.LoadXml(source)
Dim badImgs As IEnumerable(Of XmlElement) = From el In xDoc.GetElementsByTagName("img")
Select img = CType(el, XmlElement)
Where img.HasAttribute("alt") AndAlso img.Attributes("alt").Value = "badImage"

For i As Integer = 0 To badImgs.Count - 1 : badImgs(i).ParentNode.RemoveChild(badImgs(i)) : Next
Return xDoc.OuterXml

Catch ex As Exception
Stop 'Bad XML or something go wrong
End Try

Return ""

End Function

Then:

Dim myString As String = "<html><body><span>some text here..</span><img src='#' alt='goodImage'/><span>more text...</span><img src='#' alt='badImage'/></body></html>"
Dim newString As String = ClearBadImgTags(myString)

PHP remove all HTML except img tags

you can use this library or package for your solution:

PHP Simple HTML DOM Parser



Related Topics



Leave a reply



Submit