PHP:通过链接STR更换(PHP: STR replace by link)

2019-08-20 06:11发布

我有这个PHP客舱。

如果我想请在客舱的链接,它不会显示为一个链接。

如何使用STR代替做到这一点?

它应该像'HTTP 'HTTP响应的东西://' '.com' 之间 '.NL' WWW 'WWW'。 ....

我的另一个STR代替线条看起来像这样:

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

有人?

Answer 1:

嘿! 试试这个代码(在php.net发现某处):

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;
}

嗯,坏了压痕。 试试这个http://triop.se/code.txt



Answer 2:

您应该使用正则表达式来代替。 看的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);


文章来源: PHP: STR replace by link