Php remove all attributes from html tags

I have this html code:

hello

How can I remove attributes from all tags? I'd like it to look like this:

hello

asked Jun 11, 2010 at 20:43

0

Adapted from my answer on a similar question

$text = '

hello

'; echo preg_replace["/]*?[\/?]>/si",'', $text]; //

hello

The RegExp broken down:

/              # Start Pattern
 <             # Match '', Zero or More times, not-greedy [wont eat the /]
 [\/?]         # Capture Group $2 - '/' if it is there
 >             # Match '>'
/is            # End Pattern - Case Insensitive & Multi-line ability

Add some quoting, and use the replacement text it should strip any text after the tagname until the end of tag /> or just >.

Please Note This isn't necessarily going to work on ALL input, as the Anti-HTML + RegExp will tell you. There are a few fallbacks, most notably

would end up

"> and a few other broken issues... I would recommend looking at Zend_Filter_StripTags as a more full proof tags/attributes filter in PHP

T.Todua

50.1k19 gold badges217 silver badges213 bronze badges

answered Jun 11, 2010 at 21:02

gnarfgnarf

104k25 gold badges125 silver badges160 bronze badges

8

Here is how to do it with native DOM:

$dom = new DOMDocument;                 // init new DOMDocument
$dom->loadHTML[$html];                  // load HTML into it
$xpath = new DOMXPath[$dom];            // create a new XPath
$nodes = $xpath->query['//*[@style]'];  // Find elements with a style attribute
foreach [$nodes as $node] {              // Iterate over found elements
    $node->removeAttribute['style'];    // Remove style attribute
}
echo $dom->saveHTML[];                  // output cleaned HTML

If you want to remove all possible attributes from all possible tags, do

$dom = new DOMDocument;
$dom->loadHTML[$html];
$xpath = new DOMXPath[$dom];
$nodes = $xpath->query['//@*'];
foreach [$nodes as $node] {
    $node->parentNode->removeAttribute[$node->nodeName];
}
echo $dom->saveHTML[];

eozzy

63.1k103 gold badges269 silver badges412 bronze badges

answered Jun 11, 2010 at 21:38

GordonGordon

308k72 gold badges527 silver badges551 bronze badges

6

I would avoid using regex as HTML is not a regular language and instead use a html parser like Simple HTML DOM

You can get a list of attributes that the object has by using attr. For example:

$html = str_get_html['
World
']; var_dump[$html->find["div", 0]->attr]; / /* array[1] { ["id"]=> string[5] "hello" } */ foreach [ $html->find["div", 0]->attr as &$value ]{ $value = null; } print $html //
World

answered Jun 11, 2010 at 20:44

YacobyYacoby

53.3k13 gold badges111 silver badges119 bronze badges

0

$html_text = '

Hello world. Its beautiful day.

'; $strip_text = strip_tags[$html_text, '']; $result = preg_replace['/]*>/', '', $strip_text]; echo $result; // Result string 'Hello world. Its beautiful day.'

answered May 24, 2014 at 11:26

1

Another way to do it using php's DOMDocument class [without xpath] is to iterate over the attributes on a given node. Please note, due to the way php handles the DOMNamedNodeMap class, you must iterate backward over the collection if you plan on altering it. This behaviour has been discussed elsewhere and is also noted in the documentation comments. The same applies to the DOMNodeList class when it comes to removing or adding elements. To be on the safe side, I always iterate backwards with these objects.

Here is a simple example:

function scrubAttributes[$html, $attributes = []] {
    $dom = new DOMDocument[];
    $dom->loadHTML[$html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD];
    for [$els = $dom->getElementsByTagname['*'], $i = $els->length - 1; $i >= 0; $i--] {
        for [$attrs = $els->item[$i]->attributes, $ii = $attrs->length - 1; $ii >= 0; $ii--] {
            $els->item[$i]->removeAttribute[$attrs->item[$ii]->name];
        }
    }
    return $dom->saveHTML[];
}

Here's a demo: //3v4l.org/G8VPg

answered Jan 15, 2021 at 18:10

Eaten by a GrueEaten by a Grue

19.9k10 gold badges79 silver badges99 bronze badges

1

Optimized regular expression from the top rated answer on this issue:

$text = '
a is less than b: a

Chủ Đề