Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
在PHP中添加指向span标记中特定单词的链接_Php_Regex_Preg Replace_Preg Match_Highlighting - Fatal编程技术网

在PHP中添加指向span标记中特定单词的链接

在PHP中添加指向span标记中特定单词的链接,php,regex,preg-replace,preg-match,highlighting,Php,Regex,Preg Replace,Preg Match,Highlighting,我有一个单词列表,我想添加一个链接,我可以使用preg_match_all和preg_replace很容易做到这一点: $str = "<span class=\"cz\">Dám si jedno pivo prosím.</span> = I'll have a beer please."; preg_match_all('/[a-zťúůýžáčďéěíňóřš]+/i',$str,$matches); $matches = array_unique($match

我有一个单词列表,我想添加一个链接,我可以使用preg_match_all和preg_replace很容易做到这一点:

$str = "<span class=\"cz\">Dám si jedno pivo prosím.</span> = I'll have a beer please.";

preg_match_all('/[a-zťúůýžáčďéěíňóřš]+/i',$str,$matches);
$matches = array_unique($matches[0]);

foreach ($matches as $match) {
    if(!empty($words[$match])) {
        $str = preg_replace("/(^|[^\w]){1}(".preg_quote($match,"/").")($|[^\w]){1}/i", '\\1<a href="#">\\2</a>\\3', $str);
    }
}

echo $str;
$str=“我要一杯啤酒。”;
pregêu matchêu all('/[a-zťůýèċċċċċěňňêš]+/i',$str,$matches);
$matches=array_unique($matches[0]);
foreach($matches作为$match进行匹配){
如果(!empty($words[$match])){
$str=preg_replace(“/(^.[^\w]){1}(.preg_quote($match,“/”))($)[^\w]){1}/i“,'\\1\\3',$str);
}
}
echo$str;
我想做的是将链接限制为仅在span标记内

我的脑子里全是正则表达式,所以任何帮助都将不胜感激!谢谢

Darren.

/(*)([^
preg_match_all('/[a-zťůťťťŠŠŠŠŠŠŠŠŠŠŠŠŠŠŠ352;
/( *<SPAN*>)([^<]*)(<\/SPAN>)/i
preg_match_all('/[a-zťúůýžáčďéěíňóřš]+(?=\s*?</span>)/i',$str,$matches);