【发布时间】:2016-01-17 00:39:05
【问题描述】:
我正在尝试将纯文本转换为链接、主题标签和@tags。我已经设法部分做到了这一点,但找不到任何区分主题标签和包含哈希的链接的方法。
我是使用正则表达式的新手,所以可能有点乱!
//link
$message = preg_replace('/((http(s)?)(\:\/\/)|(www\.))([a-zA-Z0-9_\-\.\/\&\%\?\=\+\#\:\;\~\[\]\!\,\@\$\'\(\)\*]+)/', '<a href="http$3://$5$6">$0</a>', $message );
//handle
$message = preg_replace('/[@]+([A-Za-z0-9-_]+)/', '<a href="#$1">$1</a>', $message );
//hashtag
$message = preg_replace('/[#]+([A-Za-z0-9-_]+)/', '<a href="#$1">$1</a>', $message );
纯文本根据需要转换为链接,然后在散列点处中断。
所需文字:
www.hello.com/about_us/test%20page/test-page.php#header?this=12345&that=YES
实际文字:
header?this=12345&that=YES">www.hello.com/about_us/test%20page/test-page.php#header?this=12345&that=YES
在将其转换为主题标签之前,是否有任何方法可以检查哈希是否是 URL 的一部分?
【问题讨论】: