PHP无法转换从getElementByid PHP获取的字符串,结果为零

时间:2019-01-27 18:27:33

标签: php html dom

PHP无法读取转换为整数的字符串,结果始终为0

有人可以帮助我解决我的php代码。请帮助我。

<html> 
    <body>
        <div id='div1'></div>
        <div id='div2'>
            <H2>demo string to integer </H2>
            <p> the example of value is <span style="font-style: italic;">12</span> </p>
        <?php $shtm="<div id='a'>12</div>" ;?>
        </div>
    </body>
</html>

<?php
$doc = new DOMDocument();
$doc->loadHTML($shtm);
$div = $doc->getElementById("a");

$nval= $div->ownerDocument->saveHTML($div);

echo '1. result before converted :'. $nval .'</br>';
echo '2. using gettype : '. gettype($nval) .'</br>';
echo '3. convert to integer using (int) :' . (int)$nval.'</br>';
echo '4. try using intval function :' .intval($nval) .'</br>'; // it should be 13 but the result is the same 
echo '5. try adding with 1 : '.(int)$nval+1; // it should be 13 but the result is error A non well formed numeric value encountered 

?>

我需要可以在php正常字符串和int值中访问的值 我期望输出是我期望的,而不是零

1 个答案:

答案 0 :(得分:0)

如果您只想获取节点的内容,则没有用:

root

您可以轻松做到:

$nval= $div->ownerDocument->saveHTML($div);