When running this php script :
$doc = new DOMDocument();
$doc->loadHTMLFile("some_url.html");
$ele1 = $doc->getElementById ( "coupon" );
if($ele1->length){
$doc->getElementById ( "coupon" )->item(0)->nodeValue =$result["affiliate_name"] ;}
I get : Trying to get property of non-object
in the last line if it's not the right way to do it, how can I set the text of tag that I must extract usig its Id.
here is my some_url.html :
<div class="panel panel-success">
<div class="panel-heading">
<h3 id="coupon" class="panel-title">Coupon name 1</h3>
</div>
<p id="coupon_id" hidden>coupon id</p>
<div id="counter-up" class="panel-body">
0
</div>
</div>
Thank you
According to the official documentation, getElementById()
returns a single DOMElement
, which extends DOMNode
, which in turn has the $nodeValue
field you are trying to change. A DOMNode
is not a DOMNodeList
, so it has neither the $length
field nor the item()
method. Because of that, you aren't supposed to call item()
on that element, you can directly manipulate its $nodeValue
. To find out whether such element exists, simply test the result of getElementById()
for equality with NULL
(or use if ($element)
as a shorthand). Complete code would look as follows:
$doc = new DOMDocument();
$doc->loadHTMLFile("some_url.html");
$ele1 = $doc->getElementById("coupon");
if ($ele1) $ele1->nodeValue = $result["affiliate_name"];
Sources:
http://pl1.php.net/manual/en/domdocument.getelementbyid.php
http://pl1.php.net/manual/en/class.domelement.php
http://pl1.php.net/manual/en/class.domnode.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.