Regex允许在preg_replace中使用&符号


regex to allow ampersands in preg_replace

我需要做些什么来改变这一点,以允许在$结果中的&号?

function replace($s){
    $result = preg_replace("/[^'p{Latin}0-9'-]+/u", "", html_entity_decode($s, ENT_QUOTES));
    return $result;
}

只需将&添加到模式的排除项中。

$result = preg_replace("/[^'p{Latin}0-9&'-]+/u", "", html_entity_decode($s, ENT_QUOTES));

(注意:-应该留在最后,否则它可能被解释为描述一个字符范围,如0-9)