58413

How to convert a string to utf-8 code in php

i need a function or a simple algo to help me convert a normal string to utf-8 code

ex: string:

hello عربي

UTF-8 CODE:

68 65 6C 6C 6F 0A 0639 0631 0628 064A 0A

Answer1:

use the <strong>utf8_encode ( $data )</strong>

see its full documentation http://php.net/manual/en/function.utf8-encode.php

Answer2:

Use iconv, as shown:

$text = iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

This is also detects the strings current character set.

Answer3:

Try to use iconv funcion here is description

Answer4:

if you want to use it with arabic

you can use arabic php library at : http://ar-php.org/

Answer5:

Try This:

... $somStr = 'Hello'; utf8_encode_deep($somStr); // Converting string to utf8 print_r($somStr); ...

For more details regarding utf8_encode_deep() see this

Hope it solves this problem.

Recommend

  • Replacing empty space with preg_replace causes invalid characters with UTF-8
  • All is on UTF8 but I still got strange char like �
  • how do I echo only white space and no newline
  • iconv returns strange results
  • Retrieving a contacts notes
  • Special chars in Amazon S3 keys?
  • Encode Byte array to JPEG image in Objective-C
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • Serve file to user over http via php
  • How to create two column output from a single column
  • Compress a file with GZipStream while maintaining its meta-data
  • How can I get the choice “H2” back in the H2 consol?
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How can I include If-None-Match header in HttpRequestMessage
  • Approximate Order-Preserving Huffman Code
  • Suppressing passwd when calling sqlplus from shell script
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • Security issues with PHP's Readfile method
  • Parse a date string in a specific locale (not timezone!)
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Android fill_parent issue
  • Display issues when we change from one jquery mobile page to another in firefox
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Get object from AWS S3 as a stream
  • Java applet as stand-alone Windows application?
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • A cron job substitute?
  • json Serialization in asp
  • SQL merge duplicate rows and join values that are different
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • Reading document lines to the user (python)