Remove non-ascii characters from string

前端 未结 8 1282
遥遥无期
遥遥无期 2020-11-28 03:39

I\'m getting strange characters when pulling data from a website:

Â

How can I remove anything that isn\'t a non-extended ASCII character?

8条回答
  •  情书的邮戳
    2020-11-28 04:29

    This should be pretty straight forwards and no need for iconv function:

    // Remove all characters that are not the separator, a-z, 0-9, or whitespace
    $string = preg_replace('![^'.preg_quote('-').'a-z0-_9\s]+!', '', strtolower($string));
    // Replace all separator characters and whitespace by a single separator
    $string = preg_replace('!['.preg_quote('-').'\s]+!u', '-', $string);
    

提交回复
热议问题