PHP preg_替换匹配特殊字符,但不匹配utf8字母

PHP preg_替换匹配特殊字符,但不匹配utf8字母,php,regex,unicode,utf-8,ascii,Php,Regex,Unicode,Utf 8,Ascii,我有一些头衔,例如: should? be fenêtre! ﻟﻔﺮﻧﺴﻴﺔ-تعاني!!! 我可以使用什么正则表达式删除特殊字符,如:^ 我需要这样的标题: should-be-fenêtre ﻟﻔﺮﻧﺴﻴﺔ-تعاني 我试过了 $name = preg_replace("~[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+~", "-", $name); 但我明白了 Warning: preg_replace(): No ending delimit

我有一些头衔,例如:

should? be fenêtre!

ﻟﻔﺮﻧﺴﻴﺔ-تعاني!!!
我可以使用什么正则表达式删除特殊字符,如:^

我需要这样的标题:

should-be-fenêtre

ﻟﻔﺮﻧﺴﻴﺔ-تعاني
我试过了

$name = preg_replace("~[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+~", "-", $name);
但我明白了

Warning: preg_replace(): No ending delimiter '~' found in
谢谢

试试这个:

$text = preg_replace("/(?![.=$'€%-])\p{P}/u", "", $text);

只需更改断言以匹配您想要保留的任何Unicode字符。

您可以使用两个正则表达式去除任何不是字母或数字的内容,并将空白和破折号压缩为一个破折号:

// Replaces every non-letter, non-digit with a dash
$str = preg_replace('/(?=\P{Nd})\P{L}/u', '-', $str);

// Replaces runs of whitespace and dashes with a single dash
$str = preg_replace('/[\s-]{2,}/u', '-', $str);

“特殊字符”实际上是指标点符号?是的,所有字符都像:,?/*&^%$等。谢谢。你能说一下\P{Nd}和\P{L}做什么吗?