用 PHP 中相同域的链接替换纯文本
Replace plain text with links to the same domain in PHP
我正在用 links 替换纯文本,但我做错了。
我尝试了 preg_replace()
功能,但它似乎根本没有解决我的问题。
$string = 'This is a message with multiple links: http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';
preg_match_all('/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/u', $string, $url);
$links = $url[2];
foreach($links as $link){
$final_string = str_replace($link, '<a href="'.$link.'">'.$link.'</a>', $string);
}
echo $final_string;
请注意,三个 link 来自同一个域 http://google.com
,因此在替换第一个 link 时,其他
也会如此。
我使用的foreach
循环,用于每个link需要执行的函数(我不写它,因为它现在不重要)。
我希望能够与所有 link 分开工作,并且共享域的 link 不会互相踩踏。
我得到的输出:
This is a message with multiple links: <a href="http://google.com">http://google.com</a> <a href="http://twitter.com">http://twitter.com</a> <a href="http://google.com">http://google.com</a>/qwerty <a href="http://facebook.com">http://facebook.com</a> <a href="http://google.com">http://google.com</a>/ytrewq
我希望的输出:
This is a message with multiple links: <a href="http://google.com">http://google.com</a> <a href="http://twitter.com">http://twitter.com</a> <a href="http://google.com/qwerty">http://google.com/qwerty</a> <a href="http://facebook.com">http://facebook.com</a> <a href="http://google.com/ytrewq">http://google.com/ytrewq</a>
你会踢自己的。
$string = 'This is a message with multiple links: http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';
preg_match_all('/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/u', $string, $url);
$links = $url[2];
foreach($links as $link) {
$string = str_replace($link, '<a href="'.$link.'">'.$link.'</a>', $string);
}
echo $string;
您正在覆盖您的 $final_string 而不是替换 $string。
您应该使用 preg_replace_callback()
来简化操作。
尝试:
$string = 'This is a message with multiple links: ';
$string .= 'http://google.com ';
$string .= 'http://twitter.com ';
$string .= 'http://google.com/qwerty ';
$string .= 'http://facebook.com ';
$string .= 'http://google.com/ytrewq ';
$final_string = preg_replace_callback(
"/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/",
function ( $matches ) {
$link = trim( $matches[0] );
return " <a href='$link'>$link</a>";
},
$string
);
echo $final_string;
我改变了你声明的方式 $string
只是为了让它更容易阅读,但这并不重要。
另外,请注意,您的正则表达式中不需要任何标志,例如您正在使用的 u
。顺便说一下,这是不正确的,因为它应该是 U
,而不是 u
。
希望对你有帮助。
我正在用 links 替换纯文本,但我做错了。
我尝试了 preg_replace()
功能,但它似乎根本没有解决我的问题。
$string = 'This is a message with multiple links: http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';
preg_match_all('/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/u', $string, $url);
$links = $url[2];
foreach($links as $link){
$final_string = str_replace($link, '<a href="'.$link.'">'.$link.'</a>', $string);
}
echo $final_string;
请注意,三个 link 来自同一个域 http://google.com
,因此在替换第一个 link 时,其他
我使用的foreach
循环,用于每个link需要执行的函数(我不写它,因为它现在不重要)。
我希望能够与所有 link 分开工作,并且共享域的 link 不会互相踩踏。
我得到的输出:
This is a message with multiple links: <a href="http://google.com">http://google.com</a> <a href="http://twitter.com">http://twitter.com</a> <a href="http://google.com">http://google.com</a>/qwerty <a href="http://facebook.com">http://facebook.com</a> <a href="http://google.com">http://google.com</a>/ytrewq
我希望的输出:
This is a message with multiple links: <a href="http://google.com">http://google.com</a> <a href="http://twitter.com">http://twitter.com</a> <a href="http://google.com/qwerty">http://google.com/qwerty</a> <a href="http://facebook.com">http://facebook.com</a> <a href="http://google.com/ytrewq">http://google.com/ytrewq</a>
你会踢自己的。
$string = 'This is a message with multiple links: http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';
preg_match_all('/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/u', $string, $url);
$links = $url[2];
foreach($links as $link) {
$string = str_replace($link, '<a href="'.$link.'">'.$link.'</a>', $string);
}
echo $string;
您正在覆盖您的 $final_string 而不是替换 $string。
您应该使用 preg_replace_callback()
来简化操作。
尝试:
$string = 'This is a message with multiple links: ';
$string .= 'http://google.com ';
$string .= 'http://twitter.com ';
$string .= 'http://google.com/qwerty ';
$string .= 'http://facebook.com ';
$string .= 'http://google.com/ytrewq ';
$final_string = preg_replace_callback(
"/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/",
function ( $matches ) {
$link = trim( $matches[0] );
return " <a href='$link'>$link</a>";
},
$string
);
echo $final_string;
我改变了你声明的方式 $string
只是为了让它更容易阅读,但这并不重要。
另外,请注意,您的正则表达式中不需要任何标志,例如您正在使用的 u
。顺便说一下,这是不正确的,因为它应该是 U
,而不是 u
。
希望对你有帮助。