[英]Replacing Text link as link with preg_replace
我似乎有一个问题,用发布的网站链接替换文本链接,它没有链接。
编码:
$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
是从名为contents
的MySQL字段中提取的,包含其他文本,但我只是想链接链接。 此外 ,我还希望它不显示完整的URL,只显示主域。
更新:我们在同一页面上有另外两个preg_replaces,在它们前面找到链接到网站区域的+和#的东西,它们目前正在工作,不需要与上面的冲突:
$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text);
$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $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;
或者让它更容易阅读:
$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;
编辑 - 由于OP中的新信息而更新 -
你的hashtag正则表达式没有考虑URL中的哈希值,所以让我们解决这个问题......另外,你应该在匹配hash-tags或plus-tags之前匹配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);
或者让它更容易阅读......
$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);
再次编辑 - 添加可能有用的网址 -
你可以在这里测试正则表达式: http : //gskinner.com/RegExr/
另一个编辑
根据评论/问题,如果您想要考虑缺少http协议的网址,请使用以下内容:
$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);
或者让它更容易阅读......
$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);
示例用法
输入:(来自DB的文本 - > $ status_text)
<!-- language-all: lang-none -->
Hi, this is an example. This is a url http://stackoverflow.com/
and this is a hash reference that we want to link to an internal
post #AwesomePost123 and this one is a plus reference we want to
link to an internal post +AwesomePost123 and finally an example
of a url without the http protocol www.stackoverflow.com
输出:(运行正则表达式后)
<!-- language-all: lang-none -->
Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a>
and this is a hash reference that we want to link to an internal
post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to
link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example
of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>
你能试试这个:
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!');
在这里演示: http : //codepad.viper-7.com/hkOdCM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.