symfonydev avatar

String encoding in php

symfonydev

Published: 08 Nov 2017 › Updated: 08 Nov 2017String encoding in php

String encoding in php

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

Leave String encoding in php to:

Written by

Read more #strings posts


Best Posts From symfonydev

We have not curated any of symfonydev's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From symfonydev