PHP – Convert a string to a requested character encoding using iconv()

PHPServer Side ProgrammingProgramming

In PHP, the iconv() function is used to convert a string to the requested character encoding. It is used to perform a character set conversion on the string "string" from from_encoding to to_encoding.

Syntax

string iconv(str $from_encoding, str $to_encoding, str $string)

Parameters

The iconv() function accepts three parameters: $from_encoding, $to_encoding and $string.

  • $from_encoding− This parameter is used to specify the input charset.

  • $to_encoding− This parameter is used for the output charset.

  • $string− This parameter is used to convert the string.

Return Values

iconv() returns the converted string on success or it returns False on failure.

Example

 Live Demo

<pre>
   <?php
      // used the Dollar symbol to convert in string
      $text = "the Dollar symbol '$'";

      echo 'Original:', $text, PHP_EOL;
      echo 'TRANSLIT: ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
      echo 'IGNORE: ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
   ?>
</pre>

Output

Original:the Dollar symbol '$'
TRANSLIT: the Dollar symbol '$'
IGNORE: the Dollar symbol '$'

Example 2

 Live Demo

<pre>
   <?php
      // used the Dollar symbol to convert in string
      $string = "Indian Rupees '?'";

      echo 'Original: ', $string, PHP_EOL;
      echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1", $string), PHP_EOL;
      echo 'IGNORE: ', iconv("UTF-8", "ISO-8859-1", $string), PHP_EOL;
   ?>
</pre>

Output

Original: Indian Rupees '?'
TRANSLIT: Indian Rupees '?'
IGNORE: Indian Rupees '?'
raja
Published on 23-Aug-2021 08:17:39
Advertisements