Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove or Encode Non-UTF-8 Characters

Tags:

php

utf-8

Is there a function to remove all non UTF-8 characters from a string?

like image 318
itsme Avatar asked Aug 20 '26 17:08

itsme


1 Answers

If you have a UTF-8 string that might contain invalid characters, you can use iconv to remove those. This should work:

$text = iconv("utf-8", "utf-8//ignore", $text);

Making them visible with an arbitrary placeholder is a bit tougher - I can't think of any easy way to do that, short of walking through every byte and see whether it's a valid character. The Wikipedia article provides more info on how to do that.

like image 194
Pekka Avatar answered Aug 22 '26 14:08

Pekka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!