Php 删除内部为空的链接,添加指向不在允许列表中的站点的重定向链接

Php 删除内部为空的链接,添加指向不在允许列表中的站点的重定向链接,php,domdocument,Php,Domdocument,与img在a打开和关闭中的一个链接未显示。当我出于某种原因单击链接时,链接不会尝试加载页面。我还没有完全完成这个脚本,但到目前为止它还不起作用。到目前为止,我还不确定自己做错了什么 应删除,因为中的文本为空 代码输出: <?php $allowedURLHosts = array( 'youtube.com', 'google.com' ); $blockedURLHosts = array( 'yahoo.com', '208.71.34.142' ); function getD

与img在a打开和关闭中的一个链接未显示。当我出于某种原因单击链接时,链接不会尝试加载页面。我还没有完全完成这个脚本,但到目前为止它还不起作用。到目前为止,我还不确定自己做错了什么

应删除,因为中的文本为空

代码输出:

<?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元素没有出现是因为您使用的是nodeValue属性,它不会序列化HTML,不应该用于处理HTML


如果要分配完全相同的子级,请分配childNodes属性。

能否显示此代码的输出?
<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>