替换非utf8字符

jam*_*mes 5 php regex mysql utf-8

我想允许所有 utf8 字符,但想用空格或 - 替换非 utf8

这是字符串

Punjab me 1Train k niche 100 Sardar aa gaye..

99 Mar gaye...

1 Bach gaya
Run Code Online (Sandbox Code Playgroud)

无论我做什么,它都不会在火车后插入

它在火车上的修剪

我已经尝试过这两个链接

我想要完整的 utf8 范围插入 mysql 并用空格或 - 替换非 utf8

$string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
Run Code Online (Sandbox Code Playgroud)

适用于上述情况,但当我使用印地文/中文时,它也会替换它。所以我不能使用上面的代码

Aho*_*sik 6

试试这个: 仅使用uUnicode修饰符

    $re = "/[^(\\x20-\\x7F\\n)]+/u";
    $str = "Punjab me 1Train k niche 100 Sardar aa gaye..\n\n99 Mar gaye...\n\n1 Bach gaya";
    $subst = "";

    $result = preg_replace($re, $subst, $str);
Run Code Online (Sandbox Code Playgroud)

现场演示