PHP iconv 函数 - 删除字符串中第一个 unicode 字符之后的所有字符串

PHP iconv function - deletes all string after the first unicode character in the string

$string = @iconv("UTF-8", "UTF-8", $string);

我正在使用此代码替换字符串中的 Unicode 字符,但实际上这样做是删除字符串中第一个 Unicode 符号之后的所有字符。还有其他功能可以帮助我做到这一点吗?

我建议用 preg_replace 这样做:

preg_replace('/[\x00-\x1F\x7F]/u', '', $string);

甚至更好:

preg_replace('/[\x00-\x1F\x7F\xA0]/u', '', $string);

如果以上方法不适用于您的情况,这可能是:

preg_replace( '/[^[:cntrl:]]/', '',$string);

还有一个选项可以过滤您需要的内容,而不是删除不需要的内容。这样的事情应该有效:

filter_var($string, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);