PHP Removing HTML Tags from String

Remove all html tags from php string

use strip_tags

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

<?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?>

How to remove certain html tags in php strings

If you want to remove tags from a string in PHP (in this case, $str = "<b>England</b> is lovely";), you can use strip_tags($str); and it will return England is lovely
There is another parameter that you can add that doesn't remove certain tags, for example if $str = "<b>England</b> is my <div>city</div>"; and you type strip_tags($str, "<b>");, you will get <b>England</b> is my city.
Hope I helped!

How to remove HTML tag if it contains specific string

One way would be to use an xpath query:

*//td[contains(., 'Pricetoreplace')]/parent::tr

Here, we look for a td which text() property contains Pricetoreplace and then look up the corresponding parent tr. The latter will be removed from the DOM.


In PHP:

<?php

$html = <<<DATA
<tr><td class="some other class">some text here</td></tr>
<tr>
<td width="300" bgcolor="#cccccc" style="text-align: right;">
<strong>   Sometext<br />
</strong>
</td>
<td width="125" bgcolor="#009900" style="text-align: center;">
<strong><span style="color: rgb(255, 255, 255);">
<span style="font-size: larger;">Pricetoreplace</span>
</span>
</strong>
</td>
</tr>
DATA;

# set up the DOM
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);

# set up the xpath
$xpath = new DOMXPath($dom);

foreach ($xpath->query("*//td[contains(., 'Pricetoreplace')]/parent::tr") as $row) {
$row->parentNode->removeChild($row);
}
echo $dom->saveHTML();
?>


This yields

<tr><td class="some other class">some text here</td></tr>

How to remove specific html tags with contents in php?

strip_tags() has its limitations, and regular expressions are just not the tool to work effectively with arbitrary HTML strings (see this question)

You need to be working with something that understands HTML. i.e. DOMDocument. You could recurse down the tree finding the nodes you want, but fortunately PHP has DOMXPath that will do it for you

This snippet will load a string into DOMDocument, search for and remove all the <span> elements, and return the remainder into a string:

$str = <<<HTML
<!doctype html>
<html>
<body>
text to keep <span xxx="xxxx"><span xxx="xxx">unwanted text</span> unwanted text </span> text to keep
</body>
</html>
HTML;
$doc = new DOMDocument();
$doc->loadHTML($str);
$xpath = new DOMXPath($doc);

foreach($xpath->evaluate("//span") as $node) {
echo 'Removing: '.$node->nodeValue."<br>";
$node->parentNode->removeChild($node);
}
$output = $doc->saveHTML();
echo htmlspecialchars($output);

Output:

Removing: unwanted text unwanted text
Removing: unwanted text
<!DOCTYPE html> <html> <body> text to keep text to keep </body> </html>

Demo: https://3v4l.org/6m0e2



Related Topics



Leave a reply



Submit