PHP Function Reference

PHP utf8_encode() Function



The PHP utf8_encode() function encodes an ISO-8859-1 string to UTF-8.

Unicode is a universal standard, and has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character. UTF-8 has been used to transfer the Unicode character from one computer to another. However, it is not always possible to transfer a Unicode character to another computer reliably.

Syntax

utf8_encode(string)

Parameters

string Required. Specify an ISO-8859-1 string to encode.

Return Value

Returns the UTF-8 translation of string.

Example: utf8_encode() example

The example below shows the usage of utf8_encode() function.

<?php
$text = "\x48\x69";
 
//encoding string
echo utf8_encode($text)."\n";
 
//decoding string
echo utf8_decode($text);
?>

The output of the above code will be:

Hi
Hi

Example: applying on elements of an array

Consider one more example where all elements of an array are encoded and decoded.

<?php
$str = array("\x31", "\x32", "\x33", "\x34", "\x35", 
             "\x36", "\x37", "\x38", "\x39", "\x40",
             "\x41", "\x42", "\x43", "\x44", "\x45");
 
//encoding all elements of the array
foreach($str as $i)
  echo utf8_encode($i)." ";

echo "\n";
 
//decoding all elements of the array
foreach($str as $i)
  echo utf8_decode($i)." ";
?>

The output of the above code will be:

1 2 3 4 5 6 7 8 9 @ A B C D E 
1 2 3 4 5 6 7 8 9 @ A B C D E 

❮ PHP XML Parser Reference