[英]PHP DOMdocument echoing problem
$content = '<!--<sup><span style="font-weight:bold;color:black;">0</span></sup><br/>-->
<div class="popular-video-image">
<a href="video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>">
<img src="/images/topvideo/1.jpg" alt=""/>
</a>
<span class="popular-video-artist ellipsis"><a href="video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>" class="ellipsis">Far East Movement</a></span>
<span class="popular-video-title ellipsis"><a href="video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>" class="ellipsis">Like a G6</a></span>
</div>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('a') as $node)
{
$node->setAttribute('href', 'http://mysite.ru/' . $node->getAttribute('href'));
}
$dom->formatOutput = true;
echo $dom->saveXml($dom->documentElement);
Output: 输出:
<html>
<body>
<div class="popular-video-image">
<a href="http://mysite.ru/video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>">
<img src="/images/topvideo/1.jpg" alt=""/></a>
<span class="popular-video-artist ellipsis"><a href="http://mysite.ru/video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>" class="ellipsis">Far East Movement</a></span>
<span class="popular-video-title ellipsis"><a href="http://mysite.ru/video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>" class="ellipsis">Like a G6</a></span>
</div>
</body>
</html>
I do not want to add html and body tags. 我不想添加html和body标签。 Also do not want to tag replaced to
<lang>
同样也不想将标签替换为
<lang>
. 。
And
is also unnecessary. 也是不必要的。
I want to receive such content, which was at the entrance, only with modified links.. 我只想在入口处收到经过修改的链接的此类内容。
Sorry for bad english! 对不起,英语不好!
You are seeing
您正在看到
at the end of each line because your HTML has Windows-style line endings CR+LF
. 在每行的末尾,因为HTML具有Windows样式的行结尾
CR+LF
。 To get rid of them, run this on it before you feed it into DOMDocument
— to convert them to Unix-style line endings LF
: 要摆脱它们,请在将其输入
DOMDocument
之前对其进行处理,以将它们转换为Unix样式的行尾LF
:
$content = preg_replace('/\r\n/', "\n", $content);
saveXml takes an optional parameter to allow you to specify the node to output. saveXml使用可选参数,以允许您指定要输出的节点。
$dom->saveXml($dom->documentElement->firstChild->firstChild);
This will remove the html and body tags from the output. 这将从输出中删除html和body标签。
I guess that the <html>
and <body>
tags get placed in because you are using loadHTML
. 我猜想
<html>
和<body>
标签被放进去是因为您正在使用loadHTML
。 Try using loadXML
instead. 尝试改用
loadXML
。
As for <lang>
至于
<lang>
, it has to be replaced because otherwise the resulting XML would not be valid. ,因此必须将其替换,因为否则生成的XML将无效。 If it is causing you problems, you should change your approach a little and work with it, not against it.
如果这导致您遇到问题,则应稍微改变一下方法并使用它,而不是反对它。
<?php
$content = '<!--<sup><span style="font-weight:bold;color:black;">0</span></sup><br/>-->
<div class="popular-video-image">
<a href="video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>">
<img src="/images/topvideo/1.jpg" alt=""/>
</a>
<span class="popular-video-artist ellipsis"><a href="video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>" class="ellipsis">Far East Movement</a></span>
<span class="popular-video-title ellipsis"><a href="video/Far+East+Movement - Like+a+G6/w4s6H4ku6ZY/" title="<lang video_go_to=Far East Movement - Like a G6>" class="ellipsis">Like a G6</a></span>
</div>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('a') as $node)
{
$node->setAttribute('href', 'http://mysite.ru/' . $node->getAttribute('href'));
}
$dom->formatOutput = true;
echo preg_replace('#^<!DOCTYPE.+?>#', '', str_replace( array('<html>', '</html>', '<body>', '</body>', "\n\n", '<', '>'), array('', '', '', '', '', '<', '>',), $dom->saveHTML()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.