The iconv() function is an inbuilt function in PHP which is used to convert a string to requested character encoding. The iconv() is an international standard conversion application command-line programming interface which converts different character encodings to other encoding types with the help of Unicode conversion.
The converted module string represented by a local character set or by another character set, which is Unicode character set or other supported character set depend on the iconv implementation on the system.
Syntax:
string iconv ( string $input_charset, string $output_charset, string $str )
Parameters: This function accepts three parameters as mentioned above and described below:
- $input_charset : It is mandatory parameter used to take input characters string set.
- $output_charset : If you append the string // TRANSLIT to out_charset transliteration is activated. This means that when a character can’t be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string // IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, E_NOTICE is generated and the function will return FALSE.
- $str: The required string which to be converted.
Return Value: If a string is successfully converted into requested character encoding then it returns the converted string, otherwise, return FALSE.Examples:
Input : $str = "EURO symbol '€'"; Output : Original :EURO symbol '€' TRANSLIT :EURO symbol 'EUR' IGNORE :EURO symbol '' PLAIN : Input : $str = "Indian rupees '₹'"; Output : Original :Indian rupees '₹' TRANSLIT :Indian rupees 'INR' IGNORE :Indian rupees '' PLAIN :
Below programs illustrate the iconv() function in PHP:
Program 1:
php
<?php // Illustrate the iconv() function in php // Input string in Indian Rupees Symbol $str = "Indian Rupees '?' " ; // Print original string echo 'Original :' , ( "$str" ), PHP_EOL; // Print translating string echo 'TRANSLIT :' , iconv( "UTF-8" , "ISO-8859-1//TRANSLIT" , $str ), PHP_EOL; // Print ignoring symbol echo 'IGNORE :' , iconv( "UTF-8" , "ISO-8859-1//IGNORE" , $str ), PHP_EOL; // Print plain symbol echo 'PLAIN :' , iconv( "UTF-8" , "ISO-8859-1" , $str ), PHP_EOL; ?> |
Output:
Original :Indian rupees '₹' TRANSLIT :Indian rupees 'INR' IGNORE :Indian rupees '' PLAIN :
Note: PHP Notice: iconv(): Detected an illegal character in input string in /home/90ff059987ef1d6be3414be3dfb0c043.php on line 19
Program 2:
php
<?php // Input Euro Symbol $str = " EURO '€' " ; // Print original string echo 'Original :' , ( "$str" ), PHP_EOL; // Print translating string echo 'TRANSLIT :' , iconv( "UTF-8" , "ISO-8859-1//TRANSLIT" , $str ), PHP_EOL; // Print ignoring symbol echo 'IGNORE :' , iconv( "UTF-8" , "ISO-8859-1//IGNORE" , $str ), PHP_EOL; // Print plain symbol echo 'Plain :' , iconv( "UTF-8" , "ISO-8859-1" , $str ), PHP_EOL; ?> |
Output:
Original : EURO '€' TRANSLIT : EURO 'EUR' IGNORE : EURO '' PLAIN :
Reference: http://php.net/manual/en/function.iconv.php