Posts tagged dom
PHP DOMDocument short-tag issues
Jan 28th
Using the single quotation mark while creating new elements and passing empty string with single quotation marks will result as short tag! for example:
function createTag($tag = null, $content = null, $id = null, $class = null) {
if ($tag == null) {
return $tag;
} else {
if (!isset($content)) {
$tag = $this->dom->createElement($tag);
} else {
$patterns = array("(&)");
$replaces = array("&");
$result = preg_replace($patterns, $replaces, $content);
$tag = $this->dom->createElement($tag, $content);
}
if (isset($id)) {
if (preg_match("/(^[a-z]{1}[a-z0-9_]+)$/", $id)) {
$tag->setAttribute("id", $id);
} else {
/* todo: throw exception */
throw new Exception("Invalide id -> " . $id);
}
}
if (isset($class)) {
if (preg_match("/(^[a-z]{1}([a-z0-9_-\s])+)$/", $class)) {
$tag->setAttribute("class", $class);
} else {
/* todo: throw exception */
throw new Exception("Invalide class -> " . $class);
}
}
return $tag;
}
}
PHP DOMDocument appendHTML
Jan 26th
Appending returned document fragment is enough for this operation.
public function appendHTML($rawHtml)
{
$tmp = $this->createDocumentFragment();
$tmp->appendXML($rawHtml);
return $tmp;
}
This function might throw invalid operation exception or parsing issues if your html string has no valid syntax


LinkedIn
Twitter
Skype