donglilian0061
donglilian0061
2015-12-25 03:11
浏览 78
已采纳

PHP DOMDocument getElementsByTagName返回不完整的元素?

I'm using PHP 5.3. I have the following code:

view1.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
      <form method="post">
         <span>Hello this is some text</span>
         <input type="text" name="input1" value="mystuf"/>
         <p>Blah blah this is boring</p>
         <input type="text" name="input2" value="hello"/>
         <img src="image-of-a-kangaroo.png" />
         <input type="text" name="input3" />
         <ul>
            <li>Buy brocolli</li>
            <li>Buy oregano</li>
            <li>Buy milk</li>
         </ul>
         <input type="text" name="input4" />
         <textarea name="input100"></textarea>
         <input type="text" name="input101" />
         <p><strong>Yes, I like pizza!</strong><span>But my porcupine gets sick eating pizza.</span></p>
      </form>
</body>
</html>

index.php

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->loadHTMLFile('view1.html');
$input = $xmlDoc->getElementsByTagName('input');
foreach ($input as $i) {
        echo $i->nodeValue, PHP_EOL;
}

How come $i->nodeValue always gives empty or no result?

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHP 5.3。 我有以下代码:

view1.html

 &lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt;  
&lt; / head&gt; 
&lt; body&gt; 
&lt; form method =“post”&gt; 
&lt; span&gt;您好这是一些文字&lt; / span&gt; 
&lt; input type =“text”name =  “input1”value =“mystuf”/&gt; 
&lt; p&gt; Blah blah这是无聊的&lt; / p&gt; 
&lt; input type =“text”name =“input2”value =“hello”/&gt; \  n&lt; img src =“image-of-a-kangaroo.png”/&gt; 
&lt; input type =“text”name =“input3”/&gt; 
&lt; ul&gt; 
&lt; li&gt; 购买brocolli&lt; / li&gt; 
&lt; li&gt;购买oregano&lt; / li&gt; 
&lt; li&gt;购买牛奶&lt; / li&gt; 
&lt; / ul&gt; 
&lt; input type =“text”name =“  input4“/&gt; 
&lt; textarea name =”input100“&gt;&lt; / textarea&gt; 
&lt; input type =”text“name =”input101“/&gt; 
&lt; p&gt;&lt; strong&gt; 是的,我喜欢披萨!&lt; / strong&gt;&lt; span&gt;但是我的豪猪生病吃披萨。&lt; / spa  n&gt;&lt; / p&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

index.php

 &lt;?php 
 $ xmlDoc = new DOMDocument(); 
 $ xmlDoc-&gt; loadHTMLFile('view1.html'); 
 $ input = $ xmlDoc-&gt;  getElementsByTagName('input'); 
foreach($ input as $ i){
 echo $ i-&gt; nodeValue,PHP_EOL; 
} 
   
 
 

如何 来 $ i-&gt; nodeValue 总是空或没有结果?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutuoben6908
    doutuoben6908 2015-12-25 03:47
    已采纳

    Doing an echo $i->getAttribute('value') showed me the values I was looking for.

    点赞 评论

相关推荐