duanrao3371 2014-04-28 13:04
浏览 48
已采纳

Hashtag preg_replace

I have a web app which is creating and linking to #hashtags and I have noticed that if someone creates a #hashtag of #JB22 that the following code breaks.

Is there away in the regex I can allow numbers at the end of a #hashtag

$users = preg_replace("~(<var data-type=\"user\" class=\"userHighlight\" id=\"(.*?)\">)(.*?)(</var>)~", "<_link>$2|$3</_link> ", $start);
$tags = preg_replace("~(<var data-type=\"tag\" class=\"tagHighlight\" id=\"(.*?)\">)#(.*?)(</var>)~", "<_link>tag://$3|#$3</_link> ", $users);
$last = preg_replace("~(^|\\s)#(\\w*[a-zA-Z_]+\\w*)~", " <_link>tag://$2|#$2</_link> ", $tags);
  • 写回答

2条回答 默认 最新

  • dqczgtem06898 2014-04-28 13:07
    关注

    Replace this [a-zA-Z_]+ with [a-zA-Z_0-9]+ or if you want the numbers only at the end of the pattern you can use ([a-zA-Z_]+?[a-zA-Z_0-9]+?).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?