4

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
j0k
  • 22,600
  • 28
  • 79
  • 90
Moustapha Rifai
  • 41
  • 1
  • 1
  • 2
  • Try `iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);` – GBD Nov 12 '12 at 10:40
  • 1
    duplicate http://stackoverflow.com/questions/7979567/php-convert-any-string-to-utf-8-without-knowing-the-original-character-set-or – VDP Nov 12 '12 at 10:40
  • iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text); helpful link:-http://stackoverflow.com/questions/7979567/php-convert-any-string-to-utf-8-without-knowing-the-original-character-set-or – Ankur Saxena Nov 12 '12 at 10:46

5 Answers5

12

use the utf8_encode ( $data )

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

Omar Freewan
  • 2,678
  • 4
  • 25
  • 49
  • 1
    utf8_encode ( $data ) Work only on ISO-8859-1 code . as I see in the question , the text probably are in "windows-1256" code the easiest way is `iconv("windows-1256","utf-8",$txt);` – Salem Jun 19 '15 at 18:10
10

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.

StampyCode
  • 7,218
  • 3
  • 28
  • 44
Adam
  • 1,214
  • 13
  • 23
4

Try to use iconv funcion here is description

Sirko
  • 72,589
  • 19
  • 149
  • 183
Anton Sementsov
  • 1,196
  • 6
  • 18
  • 34
2

if you want to use it with arabic

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

Mohammad Ahmad
  • 715
  • 8
  • 22
1

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.

SajjadHashmi
  • 3,795
  • 2
  • 19
  • 22