PHP:替换文本区域中的绝对网址 [英] PHP: replacing absolute urls in a textarea
问题描述
感谢@Mihai Stancu,我得到了一个将相对 url 替换为绝对 url 的函数.我改进了它,所以它是为 href 和 src 值做的.
Thanks to @Mihai Stancu I got a function that replaces relative urls to absolute urls. I improved it so it is doing it for href and src values.
我有一个带有一个日历的域,我正在将一些事件转移到另一个域,在那里我也使用这些事件.我拥有这两个域,因此创建绝对网址没有安全风险.
I have got one domain with one calendar on and I am transferring some of the events onto another domain where I use these events too. I own both domains so there is no security risk in creating absolute urls.
但是该函数有一个错误 - 它也替换了绝对链接,所以 http://www.example.com/... 变成 http://www.example.net/http://www.example.com/...你能帮忙吗?
But the function has a bug - also it replaces absolute links so http://www.example.com/... becomes http://www.example.net/http://www.example.com/... Can you help?
如果您愿意,请随时改进功能:-)
Please feel free to improve the function if you like :-)
<?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;
?>
推荐答案
正则表达式仍然让眼睛流血.
Eyes still bleeding from the regexp.
DOMDocument 怎么样?:)
How about 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
代替.
Disclaimer: this returns a whole HTML document instead of a fragment; if you want a fragment, you can call saveHTML
on the body
tag instead.
这篇关于PHP:替换文本区域中的绝对网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!