简体   繁体   English

如何在PHP中替换字符串中的非ASCII字符?

[英]How to replace non-ASCII characters in a string in PHP?

I need to replace characters in a string which are not represented with a single byte. 我需要替换字符串中没有用单个字节表示的字符。

My string is like this 我的绳子是这样的

$inputText="centralkøkkenet kliniske diætister"; 

In that string there are characters like ø and æ . 该字符串中包含øæ之类的字符。 These characters should be replaced. 这些字符应被替换。 How do I mention these in a regular expression that I can use for replacement? 如何在可用于替换的正则表达式中提及这些内容?

If you want to replace everything other than alphanumeric and space character then try it. 如果要替换字母数字和空格字符以外的所有内容,请尝试一下。

[^a-zA-Z0-9 ]

Here is demo 这是演示

Sample code: 样例代码:

$re = "/[^a-zA-Z0-9 ]/";
$str = "centralkøkkenet kliniske diætister";
$subst = '';

$result = preg_replace($re, $subst, $str);

Better use [^\\w\\s] or [\\W\\S] to make it short and simple as suggested by @hjpotter92 as well in comments. 最好使用[^\\w\\s][\\W\\S]使它简短而简单,如@ hjpotter92以及注释中所建议。

Pattern explanation: 模式说明:

[^\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 " ")

If you want to keep also punctation ie.: -'"! ..., use this one: 如果您还想保留标点符号,例如: -'"! ...,请使用以下符号:

$text = 'central-køkkenet "kliniske" diætister!';
$new = preg_replace('/[\x7F-\xFF]/ui', '', $text);
echo $new,"\n";

output: 输出:

central-kkkenet "kliniske" ditister!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM