【问题标题】:PHP: replacing absolute urls in a textarea [closed]PHP:替换文本区域中的绝对网址 [关闭]
【发布时间】:2012-05-20 17:54:13
【问题描述】:

感谢@Mihai Stancu,我得到了一个将相对 url 替换为绝对 url 的函数。我改进了它,所以它是为 href 和 src 值做的。

我有一个带有一个日历的域,并且我正在将一些事件转移到另一个域,我也使用这些事件。我拥有这两个域,因此创建绝对 URL 没有安全风险。

但该函数有一个错误 - 它还替换了绝对链接,因此 http://www.example.com/... 变为 http://www.example.net/http://www.example.com/... 你能帮忙吗?

如果您喜欢,请随时改进功能:-)

<?php 
$domain = 'http://www.example.net/'; // notice the domain has an end slash
$textarea = 'tester afadf adf <a href="http://www.example.com/folder1/page1.html">do not replace this</a> ... bla bla <a href="/folder2/page2.html">do replace this url</a> bla bla.... <img src="http://www.example.com/somefolder/somepic.jpg" /> <img src="/somefolder/somepic.jpg" />';
$tags = array("href", "src");

foreach ($tags as $tag) { 
    $textarea = preg_replace('/'.$tag.'\s*=\s*(?<'.$tag.'>"[^\\"]*"|\'[^\\\']*\')/e', 'expand_links($tag, $domain, "$1")', $textarea);
}

function expand_links($tag, $domain, $link) {
    return($tag.'="'.$domain.trim($link, '\'"/\\').'"');
}

echo $textarea;
?>

【问题讨论】:

    标签: php url


    【解决方案1】:

    正则表达式的眼睛还在流血。

    DOMDocument 怎么样? :)

    $domain = 'http://www.example.net/'; // notice the domain has an end slash
    $textarea = 'tester afadf adf <a href="http://www.example.com/folder1/page1.html">do not replace this</a> ... bla bla <a href="/folder2/page2.html">do replace this url</a> bla bla.... <img src="http://www.example.com/somefolder/somepic.jpg" /> <img src="/somefolder/somepic.jpg" />';
    
    // wrap fragment into a full HTML body first (making sure the content type is set properly)
    $full_doc = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $textarea . '</body></html>';
    
    $d = new DOMDocument;
    libxml_use_internal_errors(true); // muffle any errors from libxml
    $d->loadHTML($textarea);
    libxml_clear_errors(); // clear the errors found
    
    $x = new DOMXPath($d);
    
    // find all tags with either href or src attribute
    foreach ($x->query('//*[@href|@src]') as $e) {
        $attr = $e->getAttributeNode('href') ?: $e->getAttributeNode('src');
    
        if (!preg_match('#^(?:https?://|mailto:)#', $attr->nodeValue)) {
            // not absolute
            $attr->nodeValue = $domain . $attr->nodeValue;
        }
    }
    
    echo $d->saveHTML();
    

    免责声明:这将返回整个 HTML 文档而不是片段;如果你想要一个片段,你可以在body标签上调用saveHTML

    【讨论】:

    • Whooooops...我的 UTF-8 格式的 $textarea 在使用此函数时会丢失其格式,并且每个丹麦字符都已损坏。
    • @Jack 现在的所见即所得编辑器在吐出有效的 XML 代码方面如何?自从我上次使用 XML/XSLT 服务器端以来已经有好几年了,我清楚地记得必须定义各种 html 实体并激活 /cleaner/ 插件才能使其正常工作。
    • 我通过这个改进解决了它:$d->loadHTML(utf8_decode($datdescription));
    • 另一个错误。它也在替换电子邮件地址;-) 你能解决这个问题吗?
    • @MihaiStancu 现在他们已经非常擅长生成半有效的 XHTML(毕竟我们还在谈论片段);尽管如此,当使用loadHTML 时,您可以避免任何杂质:)
    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多