如何从 php 中的字符串中删除像 █ 这样的特殊字符

how to remove special chars like this █ from string in php

如何从 php 中的字符串中仅删除这种类型的特殊字符 我用这个 preg_replace('/[\x00-\x1F\x80-\xC0]/u', '',$string);

但我想允许像 à,â, ', " 这样的特殊字符用于法语

您可以使用 Unicode 字符 类,例如 \p{Latin} 用于拉丁文字,\p{Sc} 用于货币,\p{P}(或更短的 \pP)用于标点字符:

$str = preg_replace('/[^0-9\p{Latin}\pP\p{Sc}@\s]+/u', '', $str);

您可以在 PCRE here 中找到不同的 unicode 字符 类。 (搜索句子:"The following general category property codes are supported"

因此,您想要使用 UTF-8 的代码点,但您想要使用 ISO 8859-15 的字符编码方案。我认为您可以将字符串转换两次:

$text = iconv("ISO-8859-15", "UTF-8", iconv("UTF-8", "ISO-8859-15//IGNORE", $text));
echo $text;