[英]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.