用指向PHP中相同域的链接替换纯文本
我正在用链接替换纯文本,但我做不好 我尝试了用指向PHP中相同域的链接替换纯文本,php,preg-replace,str-replace,Php,Preg Replace,Str Replace,我正在用链接替换纯文本,但我做不好 我尝试了preg\u replace()函数,但它似乎根本解决不了我的问题 $string='这是一条包含多个链接的消息:http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq'; preg\u match\u all('/(^\s)((http(s)\:\/\/)?[\w-]+(\.[\w-]+)+[^
preg\u replace()
函数,但它似乎根本解决不了我的问题
$string='这是一条包含多个链接的消息:http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';
preg\u match\u all('/(^\s)((http(s)\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^,\s])/u',$string,$url);
$links=$url[2];
foreach($links作为$link){
$final_string=str_replace($link,,$string);
}
echo$final_字符串;
请注意,三个链接来自同一个域http://google.com
,因此在替换第一个链接时,它会在其他链接中执行此操作
我使用的foreach
循环用于我需要为每个链接执行的函数(我不编写它,因为它现在不重要)
我希望能够分别处理所有链接,并且共享域的链接不会相互重叠
我得到的输出:
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
这是一条有多个链接的消息:/qwerty/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='这是一条包含多个链接的消息:http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';
preg\u match\u all('/(^\s)((http(s)\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^,\s])/u',$string,$url);
$links=$url[2];
foreach($links作为$link){
$string=str_replace($link,,$string);
}
echo$字符串;
您正在覆盖$final\u字符串,而不是替换$string。您应该使用
preg\u replace\u callback()
来简化操作
尝试:
$string='这是一条包含多个链接的消息:';
$string.='http://google.com ';
$string.='http://twitter.com ';
$string.='http://google.com/qwerty ';
$string.='http://facebook.com ';
$string.='http://google.com/ytrewq ';
$final\u string=preg\u replace\u回调(
“/(^ |\s)((http(s)\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^、\s])/”,
函数($matches){
$link=trim($matches[0]);
返回“”;
},
$string
);
echo$final_字符串;
我更改了您声明$string
的方式,只是为了让它更易于阅读,但这并不重要。另外,请注意,您不需要在正则表达式中使用任何标志,例如您正在使用的
u
。顺便说一下,这是不正确的,因为它应该是U
,而不是U
希望对您有所帮助。可能的副本是:
preg\u match\u all(“~\b(?:https?:/)?\S+/”,$string,$url)$links=$url[0]代码>@Toto这对我不起作用。我希望保留相同的正则表达式,因为它是我通常使用的正则表达式。这完全相同,但经过简化。@若要收到以下错误:警告:preg\u match\u all():在…中找不到结尾分隔符“~”
我仍然得到相同的结果,请尝试,您将看到http://google.com/?
链接不正确。啊。我明白了。你的代码只替换了一个事件,这一点我没有理解。难以置信,它对我来说很好!我不知道preg\u replace\u callback()
函数。非常感谢。美好的顺便说一下,我忘了从上面的代码中删除preg\u match\u all()
。我只是做了,对不起。
$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;