Example 1: The utf8_encode() function
The built-in utf8_encode() function is used to encode ISO-8859-1 string data as UTF-8 data. UTF-8 data are used to transfer Unicode characters from one location to another as follows:
This function takes a string value as an argument and returns the encoded value of the argument in utf8 format.
To follow along with this example, create a PHP file with the following script. Here, the ASCII value of some special characters is given in hex format as the argument of the function. The values of the original string and the encoded strings will be printed as output.
//Set the ASCII code as string to encode
$string_data = "\xE0\xA5\xBD";
//Encode string data
$encoded_data = utf8_encode($string_data);
//Print the original and encoded data
echo "The original string:<br />$string_data<br />";
echo "The encoded string:<br />$encoded_data<br />";
?>
Output:
The following output will be produced by executing the above script from the webserver.
Example 2: The iconv() function
The iconv() function is used to convert ASCII values or string values into a particular character encoding as follows:
This function can take three arguments. The input character set is passed as the first argument, the output character set is passed as the second argument, and the string that will be encoded is passed as the third argument.
To follow this example, create a PHP file with the following script. Here, the UTF-8 character set is given as the first argument, the ISO-8859-1 character set is given as the second argument, and a string of special characters is given as the third argument. The values of the original string and the encoded strings will be printed as output.
//Set the special symbol as string value
$string = "©«®µ¿";
//Print the original string
echo "Original Text : $string";
//Print the encoded string
echo "<br />Encoded Text : ".iconv("UTF-8", "ISO-8859-1", $string);
?>
Output:
The following output will be produced by executing the above script from the webserver.
Example 3: The mb_convert_encoding() function
The mb_convert_encoding() function is used to convert the character encoding of a string as follows:
This function can take three arguments. The first two arguments are mandatory, and the third argument is optional. The string value that will be encoded is given as the first argument, and the encoding character code is given as the second argument. The third argument is used to specify the character code name before conversion.
The mbstring extension of PHP must be installed to use the mb_convert_encoding() function for encoding. Run the following commands to enable this extension and then restart the Apache server.
$ sudo service apache2 restart
To follow this example, create a PHP file with the following script. Here, a string of some currency symbols is used as the first argument, and the UTF-7 character set is used as the second argument. The values of the original string and the encoded strings will be printed as output.
//Set the currency symbol as string value
$original_string = "€¥£";
//Print character set of the original string
echo "The original character set: ".mb_detect_encoding ($original_string)."<br />";
//Encode the string
$encoded_string = mb_convert_encoding($original_string, "UTF-7");
//Print character set of the encoded string
echo "The encoding character set: ".mb_detect_encoding ($encoded_string)."<br />";
//Print the original and encoded string
echo "The original string:<br />$original_string<br />";
echo "The encoded string:<br />$encoded_string<br />";
?>
Output:
The following output will be produced by executing the above script from the webserver.
Example 4: The base64_encode() function
The base64_encode() function is used to encode the given string in MIME base64 format. This function generates an encoded string that contains more characters than the original string as follows:
This function takes the string value as an argument and returns the encoded string as output. To follow this example, create a PHP file with the following script. A text value is given as the argument value of this function. Both the original text and the encoded text will be printed as output.
//Set the string value that will be encoded
$orginal_data = 'Welcome to LinuxHint';
//Encode the string
$encoded_data = base64_encode($orginal_data);
//Print the original and encoded data
echo "The original string:<br />$orginal_data<br /><br />";
echo "The encoded string:<br />$encoded_data<br />";
?>
Output:
The following output will be produced by executing the above script from the webserver.
Example 5: The str_rot13() function
The str_rot13() function is used to rotate every letter in the alphabet by 13 positions while ignoring non-alphabetic characters. The syntax of this function is as follows:
This function takes a string as an argument and returns an encoded string as output. To follow this example, create a PHP file with the following script. A text value is given as an argument of this function. Both the original text and the encoded text will be printed as output.
//Set the string value that will be encoded
$orginal_data = 'Welcome to LinuxHint';
//Encode the string
$encoded_data = str_rot13($orginal_data);
//Print the original and encoded data
echo "The original string:<br />$orginal_data<br /><br />";
echo "The encoded string:<br />$encoded_data<br />";
?>
Output:
The following output will be produced by executing the above script from the webserver.
Conclusion
In this tutorial, we showed you how to use various built-in PHP functions to encode special characters and normal text in PHP.