如何解决删除内部空白的链接,将重定向链接添加到不在允许列表中的站点
||<?php
$allowedURLHosts = array(
\'youtube.com\',\'google.com\'
);
$blockedURLHosts = array(
\'yahoo.com\',\'208.71.34.142\'
);
function getDomain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces[\'host\']) ? $pieces[\'host\'] : \'\';
if (preg_match(\'/(?P<domain>[a-z0-9][a-z0-9\\-]{1,63}\\.[a-z\\.]{2,6})$/i\',$domain,$regs))
{
return $regs[\'domain\'];
}
return \'\';
}
function filterLinks($str)
{
$dom = new DOMDocument;
@$dom->loadHTML($str);
global $blockedURLHosts;
global $allowedURLHosts;
// Get all links in the document.
$links = $dom->getElementsByTagName(\'a\');
$linksLength = $links->length;
// Iterate over all links.
while ($linksLength--)
{
$link = $links->item($linksLength);
if ($link->hasAttribute(\'href\'))
{
// Get the href attribute of the link.
$src = $link->getAttribute(\'href\');
if ($link->nodeValue != \'\')
{
if (in_array(getDomain($src),$blockedURLHosts))
{
$link->parentNode->removeChild($link);
}
else if (!in_array(getDomain($src),$allowedURLHosts))
{
$newlink = $dom->createElement(\'a\');
if ($link->hasAttribute(\'title\'))
{
$newlink->setAttribute(\'title\',$link->getAttribute(\'title\'));
}
$newlink->setAttribute(\'href\',\'/redirect?link=\' . urlencode($src));
$newlink->nodeValue = $link->nodeValue;
$link->parentNode->replaceChild($newlink,$link);
}
}
else
{
$link->parentNode->removeChild($link);
}
}
}
$html = \'\';
foreach($dom->getElementsByTagName(\'body\')->item(0)->childNodes as $node)
{
$html .= $dom->saveXML($node,LIBXML_NOEMPTYTAG);
}
return $html;
}
$text = \'<div class=\"InfoText\"><a href=\"http://www.getmyspacecomments.com/\"><img src=\"http://ohiok.com/img/k75/laserxpc/a1/04.jpg\" title=\"MySpace Comment Codes\" alt=\"image\" style=\"border: 0px;\"></a><br><a href=\"http://www.getmyspacecomments.com/\"><span style=\"font-size: large;\">MySpace Comments</span></a><br></div>
<div style=\"text-align: center;\"><a href=\"http://www.glitterbell.com/\" title=\"MySpace Comments\"></a><br><a href=\"http://www.glitterbell.com/\">MySpace Comments at GlitterBell.com</a><br><a href=\"http://www.myspace.com/469121002\">Add the Comment App</a></div>\';
echo filterLinks($text);
?>
一个打开和关闭中带有img的链接未显示。当我出于某些原因单击链接时,这些链接不会尝试加载页面。我仍然没有完全使用此脚本,但到目前为止,它仍无法正常工作。我不确定目前为止我在做什么错。
应删除“ 1”,因为其中的文本为空。
代码输出:
<div class=\"InfoText\"><br></br><a href=\"/redirect?link=http%3A%2F%2Fwww.getmyspacecomments.com%2F\">MySpace Comments</a><br></br></div>
<div style=\"text-align: center;\"><br></br><a href=\"/redirect?link=http%3A%2F%2Fwww.glitterbell.com%2F\">MySpace Comments at GlitterBell.com</a><br></br><a href=\"/redirect?link=http%3A%2F%2Fwww.myspace.com%2F469121002\">Add the Comment App</a></div>
解决方法
我要说的是没有显示
img
元素,因为您正在使用nodeValue
属性,该属性不会序列化HTML,因此不应用于处理HTML。
如果要分配完全相同的子代,请分配childNodes
属性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。