我需要替换字符串中没有用单个字节表示的字符。
我的字符串是这样的
$inputText="centralkøkkenet kliniske diætister";
在该字符串中有ø 和æ 等字符。这些字符应该被替换。如何在可用于替换的正则表达式中提及这些?
最佳答案
如果您想替换字母数字和空格字符以外的所有字符,请尝试。
[^a-zA-Z0-9 ]
这里是 demo
示例代码:
$re = "/[^a-zA-Z0-9 ]/";
$str = "centralkøkkenet kliniske diætister";
$subst = '';
$result = preg_replace($re, $subst, $str);
最好使用 [^\w\s]
或 [\W\S]
使其简短,正如@hjpotter92 以及评论中所建议的那样。
图案解释:
[^\w\s] any character except: word characters:
(a-z, A-Z, 0-9, _), whitespace (\n, \r, \t,\f, and " ")
[\W\S] any character of:
non-word characters (all but a-z, A-Z, 0-9, _),
non-whitespace (all but \n, \r, \t, \f, and " ")
关于php - 如何在 PHP 中替换字符串中的非 ASCII 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25236761/