Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOMDocument Namespaces

Tags:

dom

php

I'm writing a script that takes a webpage and detects how many times stuff like a facebook like button is used. Since this would best be done with a DOM, I decided to use PHP's DOMDocument.

The one problem I have come across, though, is for elements like facebook's like button:

<fb:like send="true" width="450" show_faces="true"></fb:like>

Since this element technically has a namespace of "fb", DOMDocument throws a warning saying this namespace prefix is not defined. It then proceeds to strip off the prefix, so when I get to said element, its tag is no longer fb:like, but instead, like.

Is there any way to "pre-register" a namespace? Any suggestions?

like image 607
Obto Avatar asked Jun 11 '12 18:06

Obto


1 Answers

You could use tidy to spruce things up before using an xml parser on it.

$tidy = new tidy();
$config = array(
    'output-xml'   => true, 
    'input-xml'    => true, 
    'add-xml-decl' => true,
);
$tidy->ParseString($htmlSoup, $config);
$tidy->cleanRepair();
echo $tidy;
like image 123
goat Avatar answered Oct 15 '22 09:10

goat