'PHP remove element contain specific class
I need help using preg_replace
First, I want to remove the anchor element and leave only the text. I use this regex : '#<a .*?>|#' and it seems to work.
then I want to remove an element that contains a certain class and I haven't found the right regex for it. I tried using this : '#<td class="*remove-this.*td>?#ms' but still failed
this is the code:
function prp($str)
{
$anchor = '#<a .*?>|</a>#';
$remove = '#<td class="*remove-this.*td>?#ms';
$str = preg_replace($anchor, '', $str);
$str = preg_replace($remove, '', $str);
return $str;
}
$tags = '<td class="hello there">
<a href="#" class="hy">Thank you</a>
</td>
<td class="hello there remove-this">
<a href="#" class="hy yuhu">Yuhuuu</a>
</td>';
print_r( prp($tag) );
The output i want is:
<td class="hello there">
Thank you
</td>
how to get it?
Solution 1:[1]
function prp(string $html, array $classesToRemove): string {
$doc = new DOMDocument();
$doc->loadHTML($html);
$htmlNode = $doc->childNodes[1];
$bodyNode = $htmlNode->childNodes[0];
foreach ($bodyNode->childNodes as $node) {
if (( $node->nodeType === XML_ELEMENT_NODE ) && $node->tagName === 'td') {
foreach ($node->attributes as $attribute) {
if ($attribute->nodeName === 'class' && in_array($attribute->nodeValue, $classesToRemove)) {
$bodyNode->removeChild($node);
}
}
}
}
return str_replace([ '<body>', '</body>' ], '', $doc->saveHTML($bodyNode));
}
$tags = '
<td class="hello there">
<a href="#" class="hy">Thank you</a>
</td>
<td class="hello there remove-this">
<a href="#" class="hy yuhu">Yuhuuu</a>
</td>
';
print_r(prp($tags, [ 'hello there remove-this' ]));
Solution 2:[2]
@lukas.j got me excited again and I'm trying to find information about PHP DOM
and this is the solution as expected
function prp($str)
{
$str = "<tr>{$str}</tr>";
$anchor = '#<a .*?>|</a>#';
$str = preg_replace($anchor, '', $str);
$doc = new DOMDocument;
@$doc->loadHTML($str);
$xpath = new DOMXPath($doc);
$el = $xpath->query("//td[contains(@class, 'remove-this')]");
if($el){
$el = $el[0];
$rel = $el->ownerDocument->saveXML($el);
$str = str_replace($rel, '' , $str);
}
$tr = '#<tr.*?>|</tr>#';
$str = preg_replace($tr, '', $str);
return $str;
}
$tag = '<td class="hello there">
<a href="#" class="hy">Thank you</a>
</td>
<td class="hello there remove-this">
<a href="#" class="hy yuhu">Yuhuuu</a>
</td>';
print_r( prp($tag) );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lukas.j |
| Solution 2 | E_net4 - Mr Downvoter |
