1

I'm having a problem removing attributes from the HTML tags

    $content = '<span lang="en" xml:lang="en">test</span>';

    $dom = new DOMDocument;
    $dom->loadHTML($content, LIBXML_HTML_NOIMPLIED);
    $nodes = $dom->getElementsByTagName('*');

    foreach($nodes as $node)
    {
        if ($node->hasAttribute('lang'))
        {
            $node->removeAttribute('lang');
        }

        if ($node->hasAttribute('xml:lang'))
        {
            $node->removeAttribute('xml:lang');
        }
    }

    echo $dom->saveHTML($dom->documentElement);

But the result keeps coming

    <span xml:lang="en">test</span>

Why only removes the lang attribute and don't remove the xml:lang="en"? Any ideas?

1 Answer 1

1

Probably xml:lang="en" is not corrected value of attribute in html. Change these lines:

$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED);
//code...
echo $dom->saveHTML($dom->documentElement);

to

$dom->loadXML($content, LIBXML_HTML_NOIMPLIED);
//code...
echo $dom->saveXML($dom->documentElement);
Sign up to request clarification or add additional context in comments.

Comments

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.