Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and accent characters (Ba\u015f\u00e7\u0131l)

Tags:

php

I have a string like so "Ba\u015f\u00e7\u0131l". I'm assuming those are some special accent characters. How do I:

1) Display the string with the accents (i.e replace code with actual character)

2) What is best practice for storing strings like this?

2) If I don't want to allow such characters, how do I replace it with "normal characters"?

like image 978
Andy Hin Avatar asked Oct 24 '22 17:10

Andy Hin


2 Answers

My educated guess is that you obtained such values from a JSON string. If that's the case, you should properly decode the full piece of data with json_decode():

<?php

header('Content-Type: text/plain; charset=utf-8');

$data = '"Ba\u015f\u00e7\u0131l"';
var_dump( json_decode($data) );

?>
like image 63
Álvaro González Avatar answered Oct 27 '22 09:10

Álvaro González


  1. To display the characters look at How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?

  2. You can store the character like that, or decoded, just make sure your storage can handle the UTF8 charset.

  3. Use iconv with the translit flag.

Here's an example...

function replace_unicode_escape_sequence($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
$str = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);

echo $str;

echo '<br/>';
$str = iconv('UTF8', 'ASCII//TRANSLIT', $str);

echo $str;
like image 36
Jacob Avatar answered Oct 27 '22 09:10

Jacob