从我的字符串中删除非utf8字符

时间:2019-11-13 12:26:35

标签: php utf-8

字符串:

Gentle Rain**xA0**Extra M

我想从我的字符串中删除它: xA0

我尝试了此功能,但无济于事:

    function clean_string($string) {
  $s = trim($string);
  $s = iconv("UTF-8", "UTF-8//IGNORE", $s); // drop all non utf-8 characters

  // this is some bad utf-8 byte sequence that makes mysql complain - control and formatting i think
  $s = preg_replace('/(?>[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF])/', ' ', $s);

  $s = preg_replace('/\s+/', ' ', $s); // reduce all multiple whitespace to a single space

  return $s;
}

还有其他解决方案吗?

0 个答案:

没有答案