PHP:STR替换为link

PHP:STR替换为link,php,text,hyperlink,Php,Text,Hyperlink,我有这个PHP聊天室 如果我在聊天框中键入链接,它将不会显示为链接 如何使用STR replace来执行此操作 它应该响应诸如“http”“http://”“com”“nl”“www”“www.”之类的内容 我的其他STR替换行如下所示: $bericht = str_replace ("STRING1","STRINGREPLACEMENT1",$bericht); 某人?你应该改用正则表达式。看 $regex='`(\s|\A)(http | https | ftp | ftps):/(.

我有这个PHP聊天室

如果我在聊天框中键入链接,它将不会显示为链接

如何使用STR replace来执行此操作

它应该响应诸如“http”“http://”“com”“nl”“www”“www.”之类的内容

我的其他STR替换行如下所示:

$bericht = str_replace ("STRING1","STRINGREPLACEMENT1",$bericht);

某人?

你应该改用正则表达式。看

$regex='`(\s|\A)(http | https | ftp | ftps):/(.*)(\s |\n |[,.?!])(\s |\n)|$)`ism';
$replace='$1$4'
$buffer=preg_replace($regex,$replace,$buffer);
嘿!请尝试以下代码(可以在php.net的某个地方找到):

函数格式(文本) { $tag=“rel=\”nofollow\”; // //首先,查找以http://开头且前面没有“,$text”)的字符串; // //其次,查找带有临时URL的字符串(www.something.com…),确保它们前面没有href标记或http://标记, //因为在上一步中可能会发现这一点。 //
$text=preg_replace(“/”?谢谢,这让我上路了。
$regex = '`(\s|\A)(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)`ism';
$replace = '$1<a href="$2://$3" target="_blank">$2://$3</a>$4'

$buffer = preg_replace($regex,$replace,$buffer);
function format_urldetect( $text )
{

  $tag = " rel=\"nofollow\"";
  //
  // First, look for strings beginning with http:// that AREN't preceded by an <a href tag
  //
  $text = preg_replace( "/(?<!<a href=(\"|'))((http|ftp|http)+(s)?:\/\/[^<>\s]+[\w])/i", "<a target=\"_new\" class=\"httplink\" href=\"\\0\"" . $tag . ">\\0</a>", $text );
  //
  // Second, look for strings with casual urls (www.something.com...) and make sure they don't have a href tag OR a http:// in front,
  // since that would have been caught in the previous step.
  //
  $text = preg_replace( "/(?<!<a href=(\"|')http:\/\/)(?<!http:\/\/)((www)\.[^<>\s]+[\w])/i", "<a target=\"_new\" class=\"httplink\" href=\"http://\\0\"" . $tag . ">\\0</a>", $te
xt );
  $text = preg_replace( "/(?<!<a href=(\"|')https:\/\/)(?<!http:\/\/)((www)\.[^<>\s]+[\w])/i", "<a target=\"_new\" class=\"httplink\" href=\"http://\\0\"" . $tag . ">\\0</a>", $t
ext );
  return $text;
}