String encoding in php

in strings •  7 years ago  (edited)

To recognize the pasted character, there is a website that does that for you:
http://www.mclean.net.nz/ucf/
Just copy the character and paste it into the website to get the informative description:

There are a few function in php that convert strings into different character sets:

utf8_encode() - converts a string encoded in ISO-8859-1 to UTF-8, returns the encoded string on success, or FALSE on failure.

iconv() - If you need to convert text from any encoding to any other encoding, returns the converted string or FALSE on failure.

<?php
//some German
$utf8_sentence = 'Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz';

//UK
setlocale(LC_ALL, 'en_GB');

//transliterate
$trans_sentence = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_sentence);

//gives [Weiss, Goldmann, Gobel, Weiss, Gothe, Goethe und Gotz]
//which is our original string flattened into 7-bit ASCII as
//an English speaker would do it (ie. simply remove the umlauts)
echo $trans_sentence . PHP_EOL;

//Germany
setlocale(LC_ALL, 'de_DE');

$trans_sentence = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_sentence);

//gives [Weiss, Goldmann, Goebel, Weiss, Goethe, Goethe und Goetz]
//which is exactly how a German would transliterate those
//umlauted characters if forced to use 7-bit ASCII!
//(because really ä = ae, ö = oe and ü = ue)
echo $trans_sentence . PHP_EOL;

source: http://php.net/manual/pl/function.utf8-encode.php

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!