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: https://www.php.net/manual/en/function.iconv.php
Similar Reads
PHP iconv_strlen() Function The iconv_strlen() is an inbuilt function in PHP that is used to calculate the length of a string. It is very useful when your working string is encoded in a different character set, as it can accurately determine the length regardless of the encoding. Syntax:iconv_strlen(string $string, ?string $en
2 min read
PHP iconv_substr() Function The icnov_substr() is an inbuilt function in PHP that allows you to extract a portion of a string based on a specified character encoding. Syntax: iconv_substr( string $string, int $offset, ?int $length = null, ?string $encoding = null ): string|falseParameters: This function takes four parameters t
2 min read
PHP iconv_strrpos() Function The iconv_strrpos() function is an inbuilt function in PHP that searches for the last occurrence of a substring within a string while taking the character encoding of the string into account. It is similar to the strrpos() function, but it supports multi-byte character sets. Syntax: int|false iconv_
1 min read
PHP | imagepng() Function The imagepng() function is an inbuilt function in PHP which is used to display image to browser or file. The main use of this function is to view an image in the browser, convert any other image type to PNG and applying filters to the image. Syntax: bool imagepng( resource $image, int $to, int $qual
2 min read
PHP | imagearc() Function The imagearc() function is an inbuilt function in PHP which is used to create an arc of a circle centered at the given coordinates. This function returns true on success or false on failure. Syntax: bool imagearc( $image, $cx, $cy, $width, $height, $start, $end, $color ) Parameters: This function ac
3 min read
PHP | imagearc() Function The imagearc() function is an inbuilt function in PHP which is used to create an arc of a circle centered at the given coordinates. This function returns true on success or false on failure. Syntax: bool imagearc( $image, $cx, $cy, $width, $height, $start, $end, $color ) Parameters: This function ac
3 min read