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

I have a string like "Ba \ u015f \ u00e7 \ u0131l". I guess these are special accent characters. Like me:

1) Display a line with accents (for example, replace the code with the actual character)

2) What is the best practice for storing such strings?

2) If I do not want to allow such characters, how can I replace it with "normal characters"?

+4
source share
5 answers

My educated guess is that you got such values ​​from a JSON string. If so, you should correctly decode the complete piece of data using json_decode () :

<?php header('Content-Type: text/plain; charset=utf-8'); $data = '"Ba\u015f\u00e7\u0131l"'; var_dump( json_decode($data) ); ?> 
+2
source

Here is 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; 
+1
source

Here is another option:

 <html><head> <!-- don't forget to tell the browser what encoding you're using: --> <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> </head><body><?php $string = "Ba\u015f\u00e7\u0131l"; echo json_decode('"'.str_replace('"', '\"', $string).'"'); ?></body></html> 

This works because the syntax \ u000 is what JSON uses. Note that json_decode() requires a JSON module, which is now part of the standard PHP installation.

0
source

PHP has no built-in support for decoding such strings.

There are several tricks for using the built-in function, although I'm not sure if any of them is safe and proof of injection:

Another use case for Zend Framework is to load the Zend_Utf8 offer class. See the Zend_Utf8 Offer for Zend Framework for more information.

0
source
  • Their output will lead to the issuance of the corresponding character. If you do not specify any encoding for the output document, the browser will try to guess which one will be displayed. Otherwise, you must understand this and output it explicitly.
  • Just save them or turn them into ordinary characters and binaries.
  • Use iconv functions to convert from one encoding to another, then you shuold save the source file with the desired encoding to support it.
-1
source

Source: https://habr.com/ru/post/1343712/


All Articles