PHP生成的有线HTML DOM [英] Wired HTML DOM produced by PHP
问题描述
我用这段代码检索博客的rss feed
I'm retrieving rss feed of blogs with this code
<?php
$xml = ("https://serembangirl.wordpress.com/feed/");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=5; $i++) {
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
$item_content=$x->item($i)->getElementsByTagName('encoded')->item(0)->nodeValue;
?>
<a href='#'>
<div class="card">
<div class='inner'>
<p class='title'>
<?php echo $item_title;?>
</p>
<p class='desc'> <?php echo $item_desc; ?> </p>
</div>
</div>
</a>
<?php } ?>
使用上面的代码,据说应该包装但是它产生了这个代码:
With above code, supposedly the should wrap the but it produced this instead :
http://i.imgur.com/YspeRe3.png
我真的抓住了我的脑海,解决了这个问题。
I really scratched my head solving this.
推荐答案
检查由PHP生成的实际源代码。它将在 a
中包含 div
。
Check the actual source code that is generated by PHP. It will have the div
inside the a
.
div
, p
或其他块级元素是不允许在 a
元素内。浏览器会尝试修复您的文档。
div
, p
or other block level elements are not allowed inside an a
element. The browser tries to "fix" your document.
提示1
使用XPath从DOM获取数据。
Use XPath to fetch data from the DOM.
$xpath = new DOMXPath($xmlDoc);
foreach ($xpath->evaluate('//item') as $item) {
$item_title = $xpath->evaluate('string(title)', $item);
// ...
}
提示2
如果您将数据作为HTML源输出,请不要忘记转义。
Don't forget the escaping if you output data as HTML source.
...
<p class='title'>
<?php echo htmlspecialchars($item_title); ?>
</p>
...
这篇关于PHP生成的有线HTML DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!