我有一个php变量,我需要将#value值显示为链接模式。 代码看起来像这样。
$reg_exUrl = "/\#::(.*?)/";
// The Text you want to filter for urls
$text = "This is a #simple text from which we have to perform #regex operation";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
} else {
// if no urls in the text just return the text
echo "IN Else #$".$text;
}
答案 0 :(得分:2)
通过使用\ w,您可以匹配包含字母数字字符和下划线的单词。用这个改变你的表达:
$reg_exUrl = "/#(.*?)\w+/"
答案 1 :(得分:1)
我不清楚你到底需要匹配什么。如果您想要替换#
后跟任何单词chars:
$text = "This is a #simple text from which we have to perform #regex operation";
$reg_exUrl = "/#(\w+)/";
echo preg_replace($reg_exUrl, '<a href="$0" rel="nofollow">$1</a>', $text);
//Output:
//This is a <a href="#simple" rel="nofollow">simple</a> text from which we have to perform <a href="#regex" rel="nofollow">regex</a> operation
替换使用$0
来引用匹配的文本和$1
第一组。
答案 2 :(得分:1)