php DOMDocument append element as html not text - php

the Following code will append html to element id main
I want to append content as html but it appended as text
$html='<div id="main"></div>';
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' .$html);
$el = $doc->getElementById('main');
$appended = $doc->createElement('div', '<div><h1> Hello world</h1></div>');
$el->appendChild($appended);
echo $doc->saveHTML();
current out
<h1> Hello world</h1>
but I want
Hello world

Related

appendXML stripping out img element

I need to insert an image with a div element in the middle of an article. The page is generated using PHP from a CRM. I have a routine to count the characters for all the paragraph tags, and insert the HTML after the paragraph that has the 120th character. I am using appendXML and it works, until I try to insert an image element.
When I put the <img> element in, it is stripped out. I understand it is looking for XML, however, I am closing the <img> tag which I understood would help.
Is there a way to use appendXML and not strip out the img elements?
$mcustomHTML = "<div style="position:relative; overflow:hidden;"><img src="https://s3.amazonaws.com/a.example.com/image.png" alt="No image" /></img></div>";
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $content);
// read all <p> tags and count the text until reach character 120
// then add the custom html into current node
$pTags = $doc->getElementsByTagName('p');
foreach($pTags as $tag) {
$characterCounter += strlen($tag->nodeValue);
if($characterCounter > 120) {
// this is the desired node, so put html code here
$template = $doc->createDocumentFragment();
$template->appendXML($mcustomHTML);
$tag->appendChild($template);
break;
}
}
return $doc->saveHTML();
This should work for you. It uses a temporary DOM document to convert the HTML string that you have into something workable. Then we import the contents of the temporary document into the main one. Once it's imported we can simply append it like any other node.
<?php
$mcustomHTML = '<div style="position:relative; overflow:hidden;"><img src="https://s3.amazonaws.com/a.example.com/image.png" alt="No image" /></div>';
$customDoc = new DOMDocument();
$customDoc->loadHTML($mcustomHTML, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$doc = new DOMDocument();
$doc->loadHTML($content);
$customImport = $doc->importNode($customDoc->documentElement, true);
// read all <p> tags and count the text until reach character 120
// then add the custom html into current node
$pTags = $doc->getElementsByTagName('p');
foreach($pTags as $tag) {
$characterCounter += strlen($tag->nodeValue);
if($characterCounter > 120) {
// this is the desired node, so put html code here
$tag->appendChild($customImport);
break;
}
}
return $doc->saveHTML();

how to give some words in html a link

I have this html as just example
this is some html code, and this is html
this is image <img src="any url with html word" alt="html" />
<iframe src="html"></iframe>
<script type="text/javascript">
var html = "any thing here";
var x = "this is html"
</script>
I want any way to replace all html word with html
As we see it may be in html tag attribute and we must exclude all these chance to replace and just replace this word if it plain text in span or p or div
I tried all dom ways to do that and no way
$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$query_entries = $xpath->evaluate("(//div | //span | //p)[not(ancestor::a)]/text()");
foreach($query_entries as $element){
if($element instanceof DOMText){
$element->nodeValue = str_replace('html','html',$element->nodeValue);
}
}
When I replace the nodeValue with a html it escape it and if I try to decode it it make errors in js codes
Any regex solution?

Is there a way to scrape the text that is not inside its own tags with Simple HTML DOM Parser? [duplicate]

I have the following code to retrieve all hyper links in an HTML document
and my question is how to retrieve the text nodes inside every anchor tag
(even if the text node is a child of a child like if the anchor node has a span node which has a text node)?
<?PHP
$content = "
<html>
<head>
<title>bar , this is an example</title>
</head>
<body>
<a href='aaa'><span>bbb</span></a>
</body>
</html>
";
$dom = new DOMDocument();
#$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$row = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $row->length; $i++) {
$anchor = $row->item($i);
$href = $anchor->getAttribute('href');
// I want the grab the text value which is inside the anchor
$text = //should have the value "bbb"
}
?>
Thanks
$anchor->textContent
A slightly more info here DOMNode->textContent
:D
Heres what you can do:
(string)$anchor->nodeValue;
As referenced in the DomDocument::DomNode page

PHP Simple HTML DOM parser - print class name

I have little problem with parsing some data from one webpage.
I'm trying to get class name of certain div.
Example:
< div class="stars b3"></div>
I want to save in array just b3.
Is it possible to do this?
Thanks!
See this:
<?php // http://stackoverflow.com/questions/4835300/php-dom-to-get-tag-class-with-multiple-css-class-name
$html = <<< HTML
<td class="pos" >
<a class="firstLink" href="Search/?List=200003000112097&sr=1" >
Firs link value
</a>
<br />
<a class="secondLink SecondClass" href="/Search/?KeyOpt=ALL" >
Second Link Value
</a>
</td
HTML;
$dom = new DOMDocument();
#$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate(
"/html/body//a[#class='secondLink SecondClass']"
);
echo $hrefs->item(0)->getAttribute('class');
Ref. http://codepad.org/VZVUXgrT

Rewriting HTML tags with DOM/Xpath (PHP)

I'm parsing a block of HTML with DOM/Xpath in PHP. Within this HTML, there are a few p tags that I want to convert to h4 tags, instead.
Raw HTML =>
<p class="archive">Awesome line of text</p>
Desired HTML =>
<h4>Awesome line of text</h4>
How can I do this with Xpath? I think I need to call on appendChild, but I'm not sure. Thank you for any guidance.
Something along these lines should do it:
<?php
$html = <<<END
<html>
<head>
<title>Test</title>
</head>
<body>
<p>hi</p>
<p class="archive">Awesome line of text</p>
<p>bye</p>
<p class="archive">Another line of <b>text</b></p>
<p>welcome</p>
<p class="archive">Another <u>line</u> of <b>text</b></p>
</body>
</html>
END;
$doc = new DOMDocument();
$doc->loadXML($html);
$xpath = new DOMXPath($doc);
// Find the nodes we want to change
$nodes = $xpath->query("//p[#class = 'archive']");
foreach ($nodes as $node) {
// Create a new H4 node
$h4 = $doc->createElement('h4');
// Move the children of the current node to the new one
while ($node->hasChildNodes())
$h4->appendChild($node->firstChild);
// Replace the current node with the new
$node->parentNode->replaceChild($h4, $node);
}
echo $doc->saveXML();
?>

Categories