PHP | utf8_encode() Function

The utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode 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. It is not always possible to transfer a Unicode character to another computer reliably.

Syntax:

string utf8_encode( string $string )

Parameters: This function accepts single parameter $string which is required. It specifies the ISO-8859-1 string which need to be encoded.



Return Value: This function returns a string representing the encoded string on success or False on failure.

Note: This function is available for PHP 4.0.0 and newer version.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// String to encode
$string_to_encode = "\x63";
  
// Encoding string
echo utf8_encode($string_to_encode);
  
?>

chevron_right


Output:

c

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Creating an array of 256 elements
$text = array(
    "\x20", "\x21", "\x22", "\x23", "\x24", "\x25",
    "\x26", "\x27", "\x28", "\x29", "\x2a", "\x2b",
    "\x2c", "\x2d", "\x2e", "\x2f", "\x30", "\x31"
    "\x32", "\x33", "\x34", "\x35", "\x36", "\x37"
);
  
// Encoding all characters
foreach( $text as $index ) {
    echo utf8_encode($index) . " "
}
  
?>

chevron_right


Output:

! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7

Reference: https://www.php.net/manual/en/function.utf8-encode.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.