1

First: I've read the general; don't use RegEx on XHTML arguments like this one: RegEx match open tags except XHTML self-contained tags and I do understand how RegEx will fail on nested XHTML or XML nodes.

I don't see why manipulating attributes of an XML alone should break using RegEx. So there seems to be exceptions to the general rule. Attributes are always contained in a single node starting with a < and ending with a > any other < or > in between would break the XML so such can't occur.

Now I'd like to clean an XHTML string of any microdata it might contain. That is any attributes itemscope, itemtype, itemprop, itemid and itemref. Something like this:

...
<body itemscope="itemscope" itemtype="http://schema.org/WebPage">
<div itemprop="maincontent">content</div>
...

What's the best way to do this in PHP?

Community
  • 1
  • 1
C.O.
  • 2,281
  • 6
  • 28
  • 51
  • If you want to clean the microdata attributes out of HTML, then HTMLPurifier or htmltidy might suffice. Neither knows about the html5 attributes and will remove them. – mario Jun 19 '11 at 19:54

1 Answers1

4

I'd actually suggest:

  1. Loading the string with something like SimpleXML.
  2. Removing the attributes you are interested in flushing.
  3. Saving it back to a string.

There are a bunch of namespace issues that I'm not sure how you'd have to handle, but that will probably be cleaner/happier than trying to build one or more regex expressions and make sure you don't miss anything.

EDIT: turns out SimpleXML won't work (limited modification capabilities) but DOM will. Something like this:

$data=<<<END1
<body itemscope="itemscope" itemtype="http://schema.org/WebPage">
<div itemprop="maincontent">content</div>
</body>
END1;

$xml=new DOMDocument();
$xml->loadXML($data);

// find every relevant node
$xpath = new DOMXPath($xml);
$attr = $xpath->query("//@itemscope|//@itemprop|//@itemtype");
foreach ($attr as $entry) {
  $entry->parentNode->removeAttribute($entry->nodeName);
}
echo $xml->saveXML();

You'd have to modify it to include all the attributes you want to remove, and like I said I have no clue how it would deal with namespaces, but its a start.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Looks great. One more thing, There can be meta tags in the body due to the microdata stuff. I thought I could select those with xpath 'body//meta' but somehow it does not work yet... – C.O. Jun 19 '11 at 21:05
  • The query above works on attributes: I think you want to do `//body/meta` but also realize that since those aren't attributes you can't use `removeAttribute` on them. Easiest way would be to put tags in a separate xpath loop and use `removeChild` instead. – Femi Jun 19 '11 at 21:29
  • that I understood. I did make it a separate xpath loop and did use removeChild. That's on the manual. `$xml->getElementsByTagName("body")->item(0)->getElementsByTagName("meta")->length;` gives back the actual number of tags while all xpath versions I tried even `$xpath->query("//body")->length` gives back `0`. I was wondering what my xpath problem is. Trying the same with an attribute `$xpath->query("//@itemscope")->length;`works though... – C.O. Jun 19 '11 at 21:37
  • Hard to tell exactly what you might be doing wrong without code, but `$blk = $xpath->query("//meta"); forreach($blk as $t){ $t->parentNode->removeChild($t); }` works for me. – Femi Jun 19 '11 at 22:46