【问题标题】:PHP cant conver string that is taken from getElementByid PHP , result is ZeroPHP 无法转换从 getElementByid PHP 获取的字符串,结果为零
【发布时间】:2019-06-20 20:02:05
【问题描述】:

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值中访问的值 我期望输出是我所期望的,而不是零

【问题讨论】:

标签: php html dom


【解决方案1】:

如果你只是想获取一个节点的内容是没有用的:

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

你可以简单地做:

$doc = new DOMDocument();
$doc->loadHTML($shtm);
$div = $doc->getElementById("a");
echo 'div[id="a"] contains ', $div->textContent;

【讨论】:

  • 我的目标是获取“a”元素的真正STRING值。
  • 举一个你想要的更好的例子
猜你喜欢
  • 2018-04-22
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多