Php 将文本链接替换为带有preg_replace的链接

Php 将文本链接替换为带有preg_replace的链接,php,regex,Php,Regex,我似乎有一个问题,将文本链接替换为指向已发布站点的链接,这不是链接 守则: $status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text); echo $status_text; 在这里,试试这个: $status_text = preg_replace('|([\w

我似乎有一个问题,将文本链接替换为指向已发布站点的链接,这不是链接

守则:

$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text);
echo $status_text;
在这里,试试这个:

$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
echo $status_text;
function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}
echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!');
$status\u text=preg\u replace('.|([\w\d]*)\s?(https?:/([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\]*/?)\124; i','$1',$status\u text);
回显$status_文本;
或者让它更容易阅读:

$m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$r = '$1 <a href="$2">$3</a>';

$status_text = preg_replace($m,$r,$status_text);
echo $status_text;
$m='|([\w\d]*)\s?(https?:/([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\]*/?)| i';
$r='$1';
$status\u text=preg\u replace($m,$r,$status\u text);
回显$status_文本;
编辑——根据OP中的新信息进行更新--

您的hashtag正则表达式不考虑URL中的哈希,所以让我们修复这个问题。。。 此外,在匹配哈希标记或加号标记之前,您应该先匹配URL,因为否则您将弄乱为哈希标记和加号标记创建的链接

$status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status\u text=preg\u replace('|(https?:/([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\]*/?)| i','','$status\u text);
$status_text=preg_replace(“|\B#”([\d\w|]+)| i“,”$status_text);
$status_text=preg_replace(“|\B\+([\d\w|+)| i',”$status_text);
或者让它更容易阅读

$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$2</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);
$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$3</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);
$match\u href='(https?:/([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\]*/?)\i';
$match_hash='|\B#([\d\w|]+)| i';
$match_plus='|\B\+([\d\w|+)| i';
$replace_url='';
$replace_tag='';
$status\u text=preg\u replace($match\u href,$replace\u url,$status\u text);
$status\u text=preg\u replace($match\u hash,$replace\u tag,$status\u text);
$status\u text=preg\u replace($match\u plus,$replace\u tag,$status\u text);
再次编辑——添加可能有用的URL--

您可以在此处测试正则表达式:

另一次编辑

根据注释/问题,如果要说明缺少http协议的URL,请使用以下内容:

$status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status\u text=preg\u replace('.|((https?:/)?([\d\w\.-]+.[\w\.]{2,6})[^\s\]\[\]*/?)\124; i','','.$status\u text);
$status_text=preg_replace(“|\B#”([\d\w|]+)| i“,”$status_text);
$status_text=preg_replace(“|\B\+([\d\w|+)| i',”$status_text);
或者让它更容易阅读

$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$2</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);
$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$3</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);
$match\u href='((https?:/)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\]*/?)\i';
$match_hash='|\B#([\d\w|]+)| i';
$match_plus='|\B\+([\d\w|+)| i';
$replace_url='';
$replace_tag='';
$status\u text=preg\u replace($match\u href,$replace\u url,$status\u text);
$status\u text=preg\u replace($match\u hash,$replace\u tag,$status\u text);
$status\u text=preg\u replace($match\u plus,$replace\u tag,$status\u text);
示例用法

输入:(数据库中的文本->$status\u文本)


嗨,这是一个例子。这是一个urlhttp://stackoverflow.com/ 
这是一个哈希引用,我们想链接到一个内部
post#AwesomePost123,这是我们想要的一个附加参考
链接到内部帖子+AwesomePost123,最后是一个示例
不带http协议的url www.stackoverflow.com
输出:(在运行正则表达式之后)


嗨,这是一个例子。这是一个url
这是一个哈希引用,我们想链接到一个内部
这篇文章是我们想要的附加参考
链接到内部帖子,最后是一个示例
不使用http协议的url
你能试试这个吗:

$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
echo $status_text;
function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}
echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!');

这里演示:

你能举一个你正在使用的文本的例子吗?@mason81当然,这是一个简单的文本行,例如:hello http://hello。com(此处使用空格以避免自动链接)当您说“不显示完整URL,仅显示主域”时,您是指链接的href还是链接的标签?之前已询问过此问题-搜索:“Linkify URL”。一句话:正确地做到这一点并不容易。请参阅:.@mobile-您可以通过将URL放在反引号中来避免自动链接,如下所示:
http://example.com/
。此外,您还将问题的许多重要细节添加到评论中。你应该把所有相关的东西都放在那里,这样新来的人就可以在一个地方从你的问题中得到整个问题。com和它的链接,但在http之前没有空间。我还在使用另外两个preg_替换其他东西,如#hashtags,它看起来像是去掉了thoseI的a href。我不确定你说的“在http之前没有空间”是什么意思,你能澄清一下吗?你能用一个完整的例子(包括你提到的hashtags)按照它处理的顺序发布你的其他preg_替换的代码吗?那会帮助我更好地帮助你关于那个空间。。。我编辑了我的答案,在文本和链接之间加了一个空格,这似乎是最有意义的。我本来想把它包括进去的,但显然错过了。我已经用其他preg_的替代品更新了原始链接,谢谢!我已经用另外两个preg_替代品更新了我的原始帖子,这两个替代品不需要冲突,现在就尝试一下,谢谢编辑:它会使hashlinks响应以下内容: