仅限正则表达式:数字,字母和重音符号

时间:2016-08-12 14:03:40

标签: php regex replace diacritics

我需要使用 PHP 过滤字符串以仅返回带有重音符号的“数字,字母和字母”。

我尝试了许多不同的正则表达式,但我做不到。

我最接近的是:

$string = 'Você está bem? 123 # ! @ ...';
echo preg_replace('/[^\w\s+$]/', '_', $string);

// Return: Voc__ est__ bem_ 123 _ _ _ ___
// But I need to return: Você_está_bem_123 _ _ _ ___

有人能帮助我吗?我试图解决它几个小时,甚至在这里寻找其他问题。

1 个答案:

答案 0 :(得分:7)

添加/u修饰符:

$string = 'Você está bem? 123 # ! @ ...';
echo preg_replace('/[^\w\s+$]/u', '_', $string);
                              ^

请参阅this IDEONE demo

有关SO regex文档中/u修饰符的更多信息:

  

模式和主题字符串被视为UTF-8。

相关问题