Hasen Hasen - 1 year ago 64
PHP Question

PHP convert non standard marks and special characters to normal

Is there a way to convert characters like:

É É é à Ç etc

and also this type of exclamation mark with a space after it built in:

To their normal versions. At the moment I have code like this:

$linesvalue = str_replace(["Ç","ç"],"ç",$linesvalue);
$linesvalue = str_replace(["É","É","é"],"é",$linesvalue);
$linesvalue = str_replace("è","è",$linesvalue);
$linesvalue = str_replace("à","à",$linesvalue);
$linesvalue = str_replace("â","â",$linesvalue);
$linesvalue = str_replace("ê","ê",$linesvalue);

They look like they're replacing with the same thing, but they're certainly not. Anyway, this is not too bad but I find when I try to replace the exclamation mark (!) in particular it seems to also replace some accented characters like ü and such.

Is there a way to convert the whole text in advance so its just all standard characters?

Answer Source

Use normalization form C to normalize combining marks like accents. Form KC additionally converts full-width characters like U+FF01 to standard versions.


$string = "É É é à Ç !";
print "before: $string\n";
print "hex: " . unpack("H*", $string)[1] . "\n";
$string = Normalizer::normalize($string, Normalizer::FORM_KC);
print "after: $string\n";
print "hex: " . unpack("H*", $string)[1] . "\n";


before: É É é à Ç !
hex: c3892045cc812065cc812061cc802043cca720efbc81
after: É É é à Ç !
hex: c38920c38920c3a920c3a020c3872021
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download