這裏,試試這個:
$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;
或使其easer一點點閱讀:
$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新的信息 -
你的#標籤的正則表達式沒有考慮到在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);
再次編輯 - 添加可能會有所幫助的URL -
您可以測試出的正則表達式在這裏:http://gskinner.com/RegExr/
ANOTHER編輯
按照評論/問題,如果你想要解釋缺少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>
你可以給你正在使用的文本的例子嗎? – mason81
@ mason81當然,它的一個簡單的文本行,例如: hello http:// hello。 com(在這裏避免自動鏈接的空格) – mobile
當你說你想要「不顯示完整的URL,只是主要的域名」你指的是鏈接的href或鏈接的標籤? – mason81