Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将正则表达式字符串转换为URL_Regex - Fatal编程技术网

Regex 将正则表达式字符串转换为URL

Regex 将正则表达式字符串转换为URL,regex,Regex,我正在尝试将单词转换成URL。单词之间用逗号分隔,。但是我的问题是第一个单词没有被考虑,因为没有逗号信息 public static function convertHashtags($str){ $regex = "/,+([a-zA-Z0-9_]+)/"; $str = preg_replace($regex, '<a href="'.Config::get('URL').'index/hashtag/$1">$0</a>', html

我正在尝试将单词转换成URL。单词之间用逗号分隔
。但是我的问题是第一个单词没有被考虑,因为没有逗号信息

public static function convertHashtags($str){
        $regex = "/,+([a-zA-Z0-9_]+)/";
        $str = preg_replace($regex, '<a href="'.Config::get('URL').'index/hashtag/$1">$0</a>', htmlentities($str));
        return($str);
    }
公共静态函数convertHashtags($str){
$regex=“/,+([a-zA-Z0-9_237;]+)/”;
$str=preg_replace($regex,’,htmlentities($str));
回报($str);
}

例如,
$str=june,mars,april
结果只有
mars
april
得到URLed,而不是
june

您可以将正则表达式更改为:

$regex = '/(?<=,|^)([a-zA-Z0-9_]+)/';
$regex = '/(?<=,|^)(\w+)/';

$regex='1!'/(?谢谢你的回答!但是URL现在看起来是这样的:
index/hashtag/,
它是一个逗号而不是单词。我在我的第一篇文章中犯了一个错误。单词之间没有空格。
june,mars,april
在这个例子中,
m
标志是无用的。它用来告诉
也匹配新行。因为你不是t使用
,可以将其删除。