本文整理汇总了PHP中DOMDocument::removeChild方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::removeChild方法的具体用法?PHP DOMDocument::removeChild怎么用?PHP DOMDocument::removeChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::removeChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepare
/**
* Wrap a CAS 2.0 XML response and output it as a string.
*
* @return string CAS 2.0+ server response as an XML string.
*/
public function prepare()
{
$root = $this->createElement('serviceResponse');
if (!empty($this->response)) {
$root->appendChild($this->response);
}
// Removing all child nodes from response document:
while ($this->document->firstChild) {
$this->document->removeChild($this->document->firstChild);
}
$this->document->appendChild($root);
return $this->document->saveXML();
}
示例2: convert_html_to_text
/**
* Tries to convert the given HTML into a plain text format - best suited for
* e-mail display, etc.
*
* <p>In particular, it tries to maintain the following features:
* <ul>
* <li>Links are maintained, with the 'href' copied over
* <li>Information in the <head> is lost
* </ul>
*
* @param html the input HTML
* @return the HTML converted, as best as possible, to text
*/
function convert_html_to_text($html, $width = 74)
{
$html = fix_newlines($html);
$doc = new DOMDocument('1.0', 'utf-8');
if (strpos($html, '<?xml ') === false) {
$html = '<?xml encoding="utf-8"?>' . $html;
}
# <?php (4vim)
if (!@$doc->loadHTML($html)) {
return $html;
}
// Thanks, http://us3.php.net/manual/en/domdocument.loadhtml.php#95251
// dirty fix -- remove the inserted processing instruction
foreach ($doc->childNodes as $item) {
if ($item->nodeType == XML_PI_NODE) {
$doc->removeChild($item);
// remove hack
break;
}
}
$elements = identify_node($doc);
// Add the default stylesheet
$elements->getRoot()->addStylesheet(HtmlStylesheet::fromArray(array('html' => array('white-space' => 'pre'), 'p' => array('margin-bottom' => '1em'), 'pre' => array('white-space' => 'pre'))));
$options = array();
if (is_object($elements)) {
$output = $elements->render($width, $options);
} else {
$output = $elements;
}
return trim($output);
}
示例3: parseHtml
/**
* {@inheritdoc}
*/
public function parseHtml($html, $encoding = 'UTF-8')
{
$document = new \DOMDocument();
foreach ($this->config as $name => $value) {
$document->{$name} = $value;
}
$document->encoding = $encoding;
if ($encoding !== false) {
// Tell the parser which charset to use
$encoding = $encoding ?: $document->encoding;
$encoding = '<?xml encoding="' . $encoding . '" ?>';
$html = $encoding . $html;
// @codingStandardsIgnoreStart
@$document->loadHTML($html);
// @codingStandardsIgnoreEnd
foreach ($document->childNodes as $item) {
if ($item->nodeType == XML_PI_NODE) {
$document->removeChild($item);
}
}
} else {
// @codingStandardsIgnoreStart
@$document->loadHTML($html);
// @codingStandardsIgnoreEnd
}
return $document;
}
示例4: fixChildrenClass
static function fixChildrenClass($content, $elementType, $class, $fixRawHtml = true)
{
$classPath = '\\AbstractElement\\' . $elementType;
// is this an object that extends AbstractElement?
if (is_a($content, '\\Element')) {
// is this of the right element type?
if (is_a($content, $classPath)) {
$content->addClass($class);
}
if (isset($content->contents)) {
foreach ($content->contents as $index => $value) {
$content->contents[$index] = AbstractElement\Helper::fixChildrenClass($value, $elementType, $class);
}
}
return $content;
}
if (is_string($content) && $fixRawHtml) {
$dom = new \DOMDocument();
$dom->loadHtml($content);
$reflectionClass = new \ReflectionClass($classPath);
$elements = $dom->getElementsByTagName($reflectionClass->getConstant('tag'));
foreach ($elements as $element) {
$element->setAttribute('class', $element->getAttribute('class') . ' ' . $class);
}
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild, $dom->firstChild);
return $dom->saveHTML();
}
}
示例5: parse
function parse($content)
{
$doc = new DOMDocument();
@$doc->loadHTML("<div>" . $content . "</div>");
$links = $doc->getElementsByTagName('a');
$blogurl = get_bloginfo("url");
$components = parse_url($blogurl);
$host = $components["host"];
/* Set target attribute of all external links to "_blank" */
foreach ($links as $link) {
$href = $link->getAttribute("href");
$components = parse_url($href);
if (!isset($components["host"])) {
continue;
}
if ($components["host"] != $host) {
$link->setAttribute("target", "_blank");
}
}
/*
* Extract the HTML fragment.
* Credits: http://stackoverflow.com/questions/29493678/loadhtml-libxml-html-noimplied-on-an-html-fragment-generates-incorrect-tags
*/
$temporary_wrapper = $doc->getElementsByTagName('div')->item(0);
$temporary_wrapper = $temporary_wrapper->parentNode->removeChild($temporary_wrapper);
while ($doc->firstChild) {
$doc->removeChild($doc->firstChild);
}
while ($temporary_wrapper->firstChild) {
$doc->appendChild($temporary_wrapper->firstChild);
}
/* Return HTML */
return trim($doc->saveHTML());
}
开发者ID:binarystash,项目名称:wp-outside-links-in-new-tabs,代码行数:34,代码来源:class-wp-outside-links-in-new-tabs-parser.php
示例6: DOMDocument
function preg_replace_html($content, $tags)
{
$dom = new DOMDocument();
if (!@$dom->loadHTML('<?xml encoding="UTF-8">' . $content)) {
return $content;
}
foreach ($dom->childNodes as $item) {
if ($item->nodeType === XML_PI_NODE) {
$dom->removeChild($item);
break;
}
}
$dom->encoding = 'UTF-8';
$images = $dom->getElementsByTagName('img');
$blankImage = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
for ($i = $images->length - 1; $i >= 0; $i--) {
$node = $images->item($i);
$clone = $node->cloneNode();
$noscript = $dom->createElement('noscript');
$noscript->appendChild($clone);
$node->parentNode->insertBefore($noscript, $node);
$node->setAttribute('data-src', $node->getAttribute('src'));
$node->setAttribute('src', $blankImage);
$node->setAttribute('class', trim($node->getAttribute('class') . ' lazy'));
}
$newHtml = $dom->saveHTML();
if (!$newHtml) {
return $content;
}
return $newHtml;
}
示例7: convert
/**
* Convert HTML into a textual representation
*
* @return string Text representation
*/
public function convert()
{
if (!empty($this->text)) {
return $this->text;
}
$html = $this->preprocess($this->html);
$this->document = new DOMDocument();
set_error_handler(array(__CLASS__, 'silence_errors'));
$this->document->loadHTML($html);
restore_error_handler();
// Remove the DOCTYPE
// Seems to cause segfaulting if we don't do this
if ($this->document->firstChild instanceof DOMDocumentType) {
$this->document->removeChild($this->document->firstChild);
}
$this->text = $this->parse_children($this->document->getElementsByTagName('body')->item(0));
$this->text = preg_replace("#\n{3,}#", "\n\n", $this->text);
$this->text = $this->wrap($this->text, 80);
$this->text .= $this->generate_links();
return $this->text;
}
示例8: __construct
public function __construct($document, $tagname = '*')
{
if (!is_scalar($document)) {
throw new Exception('Not a valid {JQMDoc} object');
}
$this->_namespace = microtime(true) . uniqid();
jqm_var($this->_namespace, $this);
//$document = preg_replace('/\s+/',' ',$document);
//Detect if $document is a valid full document
$hasHTML = stripos($document, '<html') !== false;
$this->__documentMap = array();
$DOM = new DOMDocument();
$DOM->recover = true;
$DOM->preserveWhiteSpace = true;
$DOM->substituteEntities = true;
$DOM->formatOutput = true;
$DOM->encoding = 'utf-8';
$DOM->loadHTML(mb_convert_encoding($document, 'HTML-ENTITIES', 'UTF-8'));
$DOM->normalizeDocument();
$html = $DOM->getElementsByTagName($tagname);
//Determine root / pieced map
$hasRoot = false;
if ($html->item(0)->childNodes->length > 0) {
$hasRoot = false;
$html_tmp = $html;
if ($html->item(0)->tagName == 'html') {
$html_tmp = $html->item(0)->childNodes->item(0);
}
if (!$hasHTML and $html_tmp->childNodes->length == 1) {
$hasRoot = true;
if ($html_tmp->childNodes->item(0)->firstChild) {
$root = $html_tmp->childNodes->item(0)->firstChild->getNodePath();
} else {
$root = $html_tmp->childNodes->item(0)->getNodePath();
}
}
}
$this->__schema['root'] = $hasRoot;
$this->__schema['rootPath'] = $root;
$this->__mapLength = false;
$this->_length = false;
$this->length = false;
if ($DOM->doctype) {
//$this->__schema['doctype'] = $DOM->saveHTML($DOM->doctype);
$DOM->removeChild($DOM->doctype);
}
//$output = $DOM->saveHTML();
//$this->__documentRaw = $output;
$this->_DOM = $DOM;
$this->_selector = $tagname;
}
示例9: DOMDocument
function hook_article_filter($article)
{
if (defined('NO_CURL') || !function_exists("curl_init")) {
return $article;
}
$charset_hack = '<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>';
$doc = new DOMDocument();
$doc->loadHTML($charset_hack . $article["content"]);
$found = false;
if ($doc) {
$xpath = new DOMXpath($doc);
$images = $xpath->query('(//img[@src])');
foreach ($images as $img) {
$src = $img->getAttribute("src");
$ch = curl_init($src);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RANGE, "0-32768");
@($result = curl_exec($ch));
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result && ($http_code == 200 || $http_code == 206)) {
$filename = tempnam(sys_get_temp_dir(), "ttsizecheck");
if ($filename) {
$fh = fopen($filename, "w");
if ($fh) {
fwrite($fh, $result);
fclose($fh);
@($info = getimagesize($filename));
if ($info && $info[0] > 0 && $info[1] > 0) {
$img->setAttribute("width", $info[0]);
$img->setAttribute("height", $info[1]);
$found = true;
}
unlink($filename);
}
}
}
}
if ($found) {
$doc->removeChild($doc->firstChild);
//remove doctype
$article["content"] = $doc->saveHTML();
}
}
return $article;
}
示例10: extProc_beforeAllWrap
/**
* @param string $item
* @param int $key
* @return string
*/
public function extProc_beforeAllWrap($item, $key)
{
if (!empty($item)) {
$pageId = $this->I['uid'];
$dom = new \DOMDocument();
$dom->loadHTML(mb_convert_encoding($item, 'HTML-ENTITIES', 'UTF-8'));
$link = $dom->getElementsByTagName('a');
$item = $link->item(0);
$dataAttribute = 'bwrk_onepage_' . $pageId;
$classAttribute = $dom->createAttribute('data-bwrkonepage-id');
$classAttribute->value = $dataAttribute;
$item->appendChild($classAttribute);
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
$newItem = $dom->saveHTML();
return $newItem;
}
}
示例11: _appendTopRecommendations
/**
* Append product recommendations to Autocomplete block html
*
* @param string $html
* @return string
*/
protected function _appendTopRecommendations($html)
{
$recommendationsModel = Mage::getModel('autocompleterecommendations/recommendation');
$query = Mage::helper('catalogsearch')->getQuery();
$productRecommendationsHtml = $recommendationsModel->getProductRecommendationsHtml($query);
if ($productRecommendationsHtml) {
$dom = new DOMDocument('1.0', 'utf8');
$dom->loadHTML($html);
$uls = $dom->getElementsByTagName('ul');
$ul = $uls->item(0);
$productRecommendationsDom = $recommendationsModel->getRecommendationsDom($productRecommendationsHtml);
$productRecommendationsDom = $dom->importNode($productRecommendationsDom, true);
$ul->appendChild($productRecommendationsDom);
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
$html = $dom->saveHTML();
}
return $html;
}
示例12: fof_item_targets
function fof_item_targets($content)
{
/* quiet warnings */
$old_xml_err = libxml_use_internal_errors(true);
$dom = new DOMDocument();
/*
Load content into DOM, within a div wrapper. Wrapper div will be
stripped before returning altered content. Without doing this,
any bare text content would get wrapped in p elements while being
parsed in.
*/
$dom->loadHtml('<div>' . mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8") . '</div>');
/* strip <!DOCTYPE> which DOMDocument adds */
$dom->removeChild($dom->firstChild);
/* strip <html><body> which DOMDocument adds */
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
/* replace or add link targets */
$xpath = new DOMXpath($dom);
foreach ($xpath->query('//a') as $node) {
$node->setAttribute('target', '_blank');
}
$content_out = '';
/* emit the updated contents inside our div */
/* start at the first node inside first div.. */
$node = $dom->firstChild->firstChild;
while ($node) {
$content_out .= $dom->saveHTML($node);
/* repeat for all nodes at this level */
$node = $node->nextSibling;
}
foreach (libxml_get_errors() as $error) {
/* just ignore warnings */
if ($error->level === LIBXML_ERR_WARNING) {
continue;
}
fof_log(__FUNCTION__ . ': ' . $error->message);
}
libxml_clear_errors();
libxml_use_internal_errors($old_xml_err);
return $content_out;
}
示例13: create
public function create($replaceHtml = false)
{
$this->_html = file_get_contents($this->getViewFile());
// this is relative root directory
$dom = new \DOMDocument();
// libxml_use_internal_errors(true);
$dom->loadHTML($this->_html);
// libxml_clear_errors();
if ($replaceHtml) {
// use this if we ever need to get rid of doc types or html tags that we for some reason get wrapped in when calling loadHTML
// remove doc type
$dom->removeChild($dom->firstChild);
// remove <html><body></body></html>
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
}
foreach ($this->getViewElements() as $viewElement) {
$viewElement->replaceHtml($dom, $this->getViewElementValue());
}
$html = $dom->saveHTML();
return $this->_html = trim($html);
}
示例14: loadHtmlNoCharset
public function loadHtmlNoCharset($htmlString = '')
{
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
if (strlen($htmlString)) {
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="UTF-8">' . $htmlString);
// dirty fix
foreach ($dom->childNodes as $item) {
if ($item->nodeType == XML_PI_NODE) {
$dom->removeChild($item);
// remove hack
break;
}
}
$dom->encoding = 'UTF-8';
// insert proper
libxml_clear_errors();
}
$this->loadDom($dom);
}
示例15: process
function process(&$article)
{
$owner_uid = $article["owner_uid"];
if (strpos($article["link"], "mrlovenstein.com") !== FALSE) {
if (strpos($article["plugin_data"], "af_comics,{$owner_uid}:") === FALSE) {
$doc = new DOMDocument();
@$doc->loadHTML($article["content"]);
if ($doc) {
$xpath = new DOMXPath($doc);
$entries = $xpath->query('(//img[@alt])');
$basenode = false;
foreach ($entries as $entry) {
// get image
$basenode = $entry->parentNode;
// add linebreak
$linebreak = $doc->createElement("br");
$basenode->appendChild($linebreak);
// add text
$alt = $entry->getAttribute("alt");
$textnode = $doc->createTextNode($alt);
$basenode->appendChild($textnode);
break;
}
if ($basenode) {
$doc->removeChild($doc->firstChild);
$article["content"] = $doc->saveHTML();
$article["plugin_data"] = "af_comics,{$owner_uid}:" . $article["plugin_data"];
}
}
} else {
if (isset($article["stored"]["content"])) {
$article["content"] = $article["stored"]["content"];
}
}
return true;
}
return false;
}