,然后将启用转写(transliteration)功能。这意味着当字符不能被目标字符集所表示时,它可以通过一个或多个形似的字符来近似表达。
       如果追加字符串
  
   Original : This is the Euro symbol '€'.
TRANSLIT : This is the Euro symbol 'EUR'.
IGNORE   : This is the Euro symbol ''.
Plain    :
Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7
   
    
     Ritchie
    
   
   
    ¶
   
   
    
     18 years ago
    
   
   
    
     
      Please note that iconv('UTF-8', 'ASCII//TRANSLIT', ...) doesn't work properly when locale category LC_CTYPE is set to C or POSIX. You must choose another locale otherwise all non-ASCII characters will be replaced with question marks. This is at least true with glibc 2.5.
      
      
      Example:
      
      
       <?php
       
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'POSIX'
      
      
       );
       
       echo
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       "Žluťoučký kůň\n"
      
      
       );
       
      
      
      
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'cs_CZ'
      
      
       );
       
       echo
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       "Žluťoučký kůň\n"
      
      
       );
       
      
      
      
       ?>
      
     
    
   
   
    
     orrd101 at gmail dot com
    
   
   
    ¶
   
   
    
     13 years ago
    
   
   
    
     
      The "//ignore" option doesn't work with recent versions of the iconv library.  So if you're having trouble with that option, you aren't alone.
      
      
      That means you can't currently use this function to filter invalid characters.  Instead it silently fails and returns an empty string (or you'll get a notice but only if you have E_NOTICE enabled).
      
      
      This has been a known bug with a known solution for at least since 2009 years but no one seems to be willing to fix it (PHP must pass the -c option to iconv).  It's still broken as of the latest release 5.4.3.
      
      
      
       https://bugs.php.net/bug.php?id=48147
      
      
      
       https://bugs.php.net/bug.php?id=52211
      
      
      
       https://bugs.php.net/bug.php?id=61484
      
      
      
      [UPDATE 15-JUN-2012]
      
      Here's a workaround...
      
      
      ini_set('mbstring.substitute_character', "none");
      
      $text= mb_convert_encoding($text, 'UTF-8', 'UTF-8');
      
      
      That will strip invalid characters from UTF-8 strings (so that you can insert it into a database, etc.).  Instead of "none" you can also use the value 32 if you want it to insert spaces in place of the invalid characters.
     
    
    
   
    
     daniel dot rhodes at warpasylum dot co dot uk
    
   
   
    ¶
   
   
    
     14 years ago
    
   
   
    
     
      Interestingly, setting different target locales results in different, yet appropriate, transliterations. For example:
      
      
      
       <?php
       
      
      
      
       $utf8_sentence
      
      
       =
      
      
       'Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz'
      
      
       ;
       
       
      
      
      
       setlocale
      
      
       (
      
      
       LC_ALL
      
      
       ,
      
      
       'en_GB'
      
      
       );
       
       
      
      
      
       $trans_sentence
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       $utf8_sentence
      
      
       );
       
       
      
      
      
       echo
      
      
       $trans_sentence
      
      
       .
      
      
       PHP_EOL
      
      
       ;
       
       
      
      
      
       setlocale
      
      
       (
      
      
       LC_ALL
      
      
       ,
      
      
       'de_DE'
      
      
       );
       
       
      
      
       $trans_sentence
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       $utf8_sentence
      
      
       );
       
       
      
      
      
       echo
      
      
       $trans_sentence
      
      
       .
      
      
       PHP_EOL
      
      
       ;
       
       
      
      
       ?>
      
     
    
   
   
    
     annuaireehtp at gmail dot com
    
   
   
    ¶
   
   
    
     15 years ago
    
   
   
    
     
      to test different combinations of convertions between charsets (when we don't know the source charset and what is the convenient destination charset) this is an example :
      
      
      
       <?php
       
       $tab
      
      
       = array(
      
      
       "UTF-8"
      
      
       ,
      
      
       "ASCII"
      
      
       ,
      
      
       "Windows-1252"
      
      
       ,
      
      
       "ISO-8859-15"
      
      
       ,
      
      
       "ISO-8859-1"
      
      
       ,
      
      
       "ISO-8859-6"
      
      
       ,
      
      
       "CP1256"
      
      
       );
       
      
      
       $chain
      
      
       =
      
      
       ""
      
    
      
       ;
       
       foreach (
      
      
       $tab
      
      
       as
      
      
       $i
      
      
       )
       
       {
       
       foreach (
      
      
       $tab
      
      
       as
      
      
       $j
      
      
       )
       
       {
       
      
      
       $chain
      
      
       .=
      
      
       "
      
      
       $i$j
      
      
       "
      
      
       .
      
      
       iconv
      
      
       (
      
      
       $i
      
      
       ,
      
      
       $j
      
      
       ,
      
      
       "
      
      
       $my_string
      
      
       "
      
      
       );
       
       }
       
       }
       
       
       echo
      
      
       $chain
      
      
       ;
       
      
      
       ?>
       
      
      
      then after displaying, you use the $i$j that shows good displaying.
      
      NB: you can add other charsets to $tab  to test other cases.
     
    
   
   
    
     Daniel Klein
    
   
   
    ¶
   
   
    
     5 years ago
    
   
   
    
     
      If you want to convert to a Unicode encoding without the byte order mark (BOM), add the endianness to the encoding, e.g. instead of "UTF-16" which will add a BOM to the start of the string, use "UTF-16BE" which will convert the string without adding a BOM.
      
      
      i.e.
      
      
      
       <?php
       
       iconv
      
      
       (
      
      
       'CP1252'
      
      
       ,
      
      
       'UTF-16'
      
      
       ,
      
      
       $text
      
      
       );
      
      
      
       iconv
      
      
       (
      
      
       'CP1252'
      
      
       ,
      
      
       'UTF-16BE'
      
      
       ,
      
      
       $text
      
      
       );
      
      
     
    
   
   
    
     zhawari at hotmail dot com
    
   
   
    ¶
   
   
    
     20 years ago
    
   
   
    
     
      Here is how to convert UCS-2 numbers to UTF-8 numbers in hex:
      
      
      
       <?php
       
      
      
       function
      
      
       ucs2toutf8
      
      
       (
      
      
       $str
      
      
       )
       
       {
       
       for (
      
      
       $i
      
      
       =
      
      
       0
      
      
       ;
      
      
       $i
      
      
       <
      
      
       strlen
      
      
       (
      
      
       $str
      
      
       );
      
      
       $i
      
      
       +=
      
      
       4
      
      
       )
       
       {
       
      
      
       $substring1
      
      
       =
      
      
       $str
      
      
       [
      
      
       $i
      
      
       ].
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       1
      
      
       ];
       
      
      
       $substring2
      
      
       =
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       2
      
      
       ].
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       3
      
      
       ];
       
       
       if (
      
      
       $substring1
      
      
       ==
      
      
       "00"
      
      
       )
       
       {
       
      
      
       $byte1
      
      
       =
      
      
       ""
      
      
       ;
       
      
      
       $byte2
      
      
       =
      
      
       $substring2
      
      
       ;
       
       }
       
       else
       
       {
       
      
      
       $substring
      
      
       =
      
      
       $substring1
      
      
       .
      
      
       $substring2
      
      
       ;
       
      
      
       $byte1
      
      
       =
      
      
       dechex
      
      
       (
      
      
       192
      
      
       +(
      
      
       hexdec
      
      
       (
      
      
       $substring
      
      
       )/
      
      
       64
      
      
       ));
       
      
      
       $byte2
      
      
       =
      
      
       dechex
      
      
       (
      
      
       128
      
      
       +(
      
      
       hexdec
      
      
       (
      
      
       $substring
      
      
       )%
      
      
       64
      
      
       ));
       
       }
       
      
      
       $utf8
      
      
       .=
      
      
       $byte1
      
      
       .
      
      
       $byte2
      
      
       ;
       
       }
       
       return
      
      
       $utf8
      
      
       ;
       
       }
       
       
       echo
      
      
       strtoupper
      
      
       (
      
      
       ucs2toutf8
      
      
       (
      
      
       "06450631062D0020"
      
      
       ));
       
       
      
      
       ?>
       
      
      
      Input:
      
      06450631062D
      
      Output:
      
      D985D8B1D8AD
      
      
      regards,
      
      Ziyad
     
    
   
   
    
     manuel at kiessling dot net
    
   
   
    ¶
   
   
    
     16 years ago
    
   
   
    
     
      Like many other people, I have encountered massive problems when using iconv() to convert between encodings (from UTF-8 to ISO-8859-15 in my case), especially on large strings.
      
      
      The main problem here is that when your string contains illegal UTF-8 characters, there is no really straight forward way to handle those. iconv() simply (and silently!) terminates the string when encountering the problematic characters (also if using //IGNORE), returning a clipped string. The
      
      
      
       <?php
       
       
       $newstring
      
      
       =
      
      
       html_entity_decode
      
      
       (
      
      
       htmlentities
      
      
       (
      
      
       $oldstring
      
      
       ,
      
      
       ENT_QUOTES
      
      
       ,
      
      
       'UTF-8'
      
      
       ),
      
      
       ENT_QUOTES
      
      
       ,
      
      
       'ISO-8859-15'
      
      
       );
       
       
      
      
       ?>
       
      
      
      workaround suggested here and elsewhere will also break when encountering illegal characters, at least dropping a useful note ("htmlentities(): Invalid multibyte sequence in argument in...")
      
      
      I have found a lot of hints, suggestions and alternative methods (it's scary and in my opinion no good sign how many ways PHP natively provides to convert the encoding of strings), but none of them really worked, except for this one:
      
      
      
       <?php
       
       
       $newstring
      
      
       =
      
      
       mb_convert_encoding
      
      
       (
      
      
       $oldstring
      
      
       ,
      
      
       'ISO-8859-15'
      
      
       ,
      
      
       'UTF-8'
      
      
       );
       
       
      
      
       ?>
      
     
    
   
   
    
     jorortega at gmail dot com
    
   
   
    ¶
   
   
    
     12 years ago
    
   
   
    
     
      Be aware that iconv in PHP uses system implementations of locales and languages, what works under linux, normally doesn't in windows.
      
      
      Also, you may notice that recent versions of linux (debian, ubuntu, centos, etc) the //TRANSLIT option doesn't work. since most distros doesn't include the intl packages (example: php5-intl and icuxx (where xx is a number) in debian) by default. And this because the intl package conflicts with another package needed for international DNS resolution.
      
      
      Problem is that configuration is dependent of the sysadmin of the machine where you're hosted, so iconv is pretty much useless by default,  depending on what configuration is used by your distro or the machine's admin.
     
    
   
   
    
     Leigh Morresi
    
   
   
    ¶
   
   
    
     16 years ago
    
   
   
    
     
      If you are getting question-marks in your iconv output when transliterating, be sure to 'setlocale' to something your system supports.
      
      
      Some PHP CMS's will default setlocale to 'C', this can be a problem.
      
      
      use the "locale" command to find out a list..
      
      
      $ locale -a
      
      C
      
      en_AU.utf8
      
      POSIX
      
      
      
       <?php
       
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'en_AU.utf8'
      
      
       );
       
      
      
       $str
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       "Côte d'Ivoire"
      
      
       );
       
      
      
       ?>
      
     
    
   
   
    
     anton dot vakulchik at gmail dot com
    
   
   
    ¶
   
   
    
     17 years ago
    
   
   
    
     
      function detectUTF8($string)
      
      {
      
      return preg_match('%(?:
      
      [\xC2-\xDF][\x80-\xBF]        # non-overlong 2-byte
      
      |\xE0[\xA0-\xBF][\x80-\xBF]               # excluding overlongs
      
      |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}      # straight 3-byte
      
      |\xED[\x80-\x9F][\x80-\xBF]               # excluding surrogates
      
      |\xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
      
      |[\xF1-\xF3][\x80-\xBF]{3}                  # planes 4-15
      
      |\xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
      
      )+%xs', $string);
      
      }
      
      
      function cp1251_utf8( $sInput )
      
      {
      
      $sOutput = "";
      
      
      for ( $i = 0; $i < strlen( $sInput ); $i++ )
      
      {
      
      $iAscii = ord( $sInput[$i] );
      
      
      if ( $iAscii >= 192 && $iAscii <= 255 )
      
      $sOutput .=  "&#".( 1040 + ( $iAscii - 192 ) ).";";
      
      else if ( $iAscii == 168 )
      
      $sOutput .= "&#".( 1025 ).";";
      
      else if ( $iAscii == 184 )
      
      $sOutput .= "&#".( 1105 ).";";
      
      else
      
      $sOutput .= $sInput[$i];
      
      }
      
      
      return $sOutput;
      
      }
      
      
      function encoding($string){
      
      if (function_exists('iconv')) {
      
      if (@!iconv('utf-8', 'cp1251', $string)) {
      
      $string = iconv('cp1251', 'utf-8', $string);
      
      }
      
      return $string;
      
      } else {
      
      if (detectUTF8($string)) {
      
      return $string;
      
      } else {
      
      return cp1251_utf8($string);
      
      }
      
      }
      
      }
      
      echo encoding($string);
     
    
   
   
    
     nikolai-dot-zujev-at-gmail-dot-com
    
   
   
    ¶
   
   
    
     20 years ago
    
   
    
   
    
     
      Here is an example how to convert windows-1251 (windows) or cp1251(Linux/Unix) encoded string to UTF-8 encoding.
      
      
      
       <?php
       
      
      
       function
      
      
       cp1251_utf8
      
      
       (
      
      
       $sInput
      
      
       )
       
       {
       
      
      
       $sOutput
      
      
       =
      
      
       ""
      
      
       ;
       
       
       for (
      
      
       $i
      
      
       =
      
      
       0
      
      
       ;
      
      
       $i
      
      
       <
      
      
       strlen
      
      
       (
      
      
       $sInput
      
      
       );
      
      
       $i
      
      
       ++ )
       
       {
       
      
      
       $iAscii
      
      
       =
      
      
       ord
      
      
       (
      
      
       $sInput
      
      
       [
      
      
       $i
      
      
       ] );
       
       
       if (
      
      
       $iAscii
      
      
       >=
      
      
       192
      
      
       &&
      
      
       $iAscii
      
      
       <=
      
      
       255
      
      
       )
       
      
      
       $sOutput
      
      
       .=
      
      
       "&#"
      
      
       .(
      
      
       1040
      
      
       + (
      
      
       $iAscii
      
      
       -
      
      
       192
      
      
       ) ).
      
      
       ";"
      
      
       ;
       
       else if (
      
      
       $iAscii
      
      
       ==
      
      
       168
      
      
       )
       
      
      
       $sOutput
      
      
       .=
      
      
       "&#"
      
      
       .(
      
      
       1025
      
      
       ).
      
      
       ";"
      
      
       ;
       
       else if (
      
      
       $iAscii
      
      
       ==
      
      
       184
      
      
       )
       
      
      
       $sOutput
      
      
       .=
      
      
       "&#"
      
      
       .(
      
      
       1105
      
      
       ).
      
      
       ";"
      
      
       ;
       
       else
       
      
      
       $sOutput
      
      
       .=
      
      
       $sInput
      
      
       [
      
      
       $i
      
      
       ];
       
       }
       
       
       return
      
      
       $sOutput
      
      
       ;
       
       }
       
      
      
       ?>
      
     
    
   
   
    
     gree:.. (gree 4T grees D0T net)
    
   
   
    ¶
   
   
    
     18 years ago
    
   
   
    
     
      In my case, I had to change:
      
      
       <?php
       
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'cs_CZ'
      
      
       );
       
      
      
       ?>
       
      
      to
      
      
       <?php
       
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'cs_CZ.UTF-8'
      
      
       );
       
      
      
       ?>
       
      
      Otherwise it returns question marks.
      
      
      When I asked my linux for locale (by locale command) it returns "cs_CZ.UTF-8", so there is maybe correlation between it.
      
      
      iconv (GNU libc) 2.6.1
      
      glibc 2.3.6
     
    
   
   
    
     atelier at degoy dot com
    
   
   
    ¶
   
   
    
     10 years ago
    
   
   
    
     
      There may be situations when a new version of a web site, all in UTF-8, has to display some old data remaining in the database with ISO-8859-1 accents. The problem is iconv("ISO-8859-1", "UTF-8", $string) should not be applied if $string is already UTF-8 encoded.
      
      
      I use this function that does'nt need any extension :
      
      
      function convert_utf8( $string ) {
      
      if ( strlen(utf8_decode($string)) == strlen($string) ) {
      
      // $string is not UTF-8
      
      return iconv("ISO-8859-1", "UTF-8", $string);
      
      } else {
      
      // already UTF-8
      
      return $string;
      
      }
      
      }
      
      
      I have not tested it extensively, hope it may help.
     
    
   
   
    
     ameten
    
   
   
    ¶
   
   
    
     14 years ago
    
   
   
    
     
      I have used iconv to convert from cp1251 into UTF-8. I spent a day to investigate why a string with Russian capital 'Р' (sounds similar to 'r') at the end cannot be inserted into a database.
      
      
      The problem is not in iconv. But 'Р' in cp1251 is chr(208) and 'Р' in UTF-8 is chr(208).chr(106). chr(106) is one of the space symbol which match '\s' in regex. So, it can be taken by a greedy '+' or '*' operator. In that case, you loose 'Р' in your string.
      
      
      For example, 'ГР   ' (Russian, UTF-8). Function preg_match. Regex is '(.+?)[\s]*'. Then '(.+?)' matches 'Г'.chr(208) and '[\s]*' matches chr(106).'   '.
      
      
      Although, it is not a bug of iconv, but it looks like it very much. That's why I put this comment here.
     
    
   
   
    
     phpnet at dariosulser dot ch
    
   
   
    ¶
   
   
    
     5 years ago
    
   
   
    
     
      ANSI = Windows-1252 = CP1252
      
      So UTF-8 -> ANSI:
      
      
      
       <?php
       
       $string
      
      
       =
      
      
       "Winkel γ=200 für 1€"
      
      
       ;
      
      
      
       $result
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'CP1252//IGNORE'
      
      
       ,
      
      
       $string
      
      
       );
       
       echo
      
      
       $result
      
      
       ;
       
      
      
       ?>
       
      
      
      Note1
      
      
       <?php
       
       $string
      
      
       =
      
      
       "Winkel γ=200 für 1€"
      
      
       ;
       
      
      
       $result
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'CP1252'
      
      
       ,
      
      
       $string
      
      
       );
       
       echo
      
      
       $result
      
      
       ;
      
      
      
       ?>
       
      
      
      Note2 (ANSI is better than decode in ISO 8859-1 (ISO-8859-1==Latin-1)
      
      
       <?php
       
       $string
      
      
       =
      
      
       "Winkel γ=200 für 1€"
      
      
       ;
       
      
      
       $result
      
      
       =
      
      
       utf8_decode
      
      
       (
      
      
       $string
      
      
       );
       
       echo
      
      
       $result
      
      
       ;
      
      
      
       ?>
       
      
      
      Note3 of used languages on Websites:
      
      93.0% = UTF-8;
      
      3.5% = Latin-1;
      
      0.6% = ANSI <----- you shoud use (or utf-8 if your page is in Chinese or has Maths)
     
    
   
   
    
     Nopius
    
   
   
    ¶
   
   
    
     10 years ago
    
   
   
    
     
      As orrd101 said, there is a bug with //IGNORE in recent PHP versions (we use 5.6.5) where we couldn't convert some strings (i.e. "∙" from UTF8 to CP1251 with //IGNORE).
      
      But we have found a workaround and now we use both //TRANSLIT and //IGNORE flags:
      
      $text="∙";
      
      iconv("UTF8", "CP1251//TRANSLIT//IGNORE", $text);
     
    
   
   
    
     ng4rrjanbiah at rediffmail dot com
    
   
   
    ¶
   
   
    
     21 years ago
    
   
   
    
     
      Here is a code to convert ISO 8859-1 to UTF-8 and vice versa without using iconv.
      
      
      
       <?php
       
      
      
      
       $str_iso8859_1
      
      
       =
      
      
       'foo in ISO 8859-1'
      
      
       ;
       
      
      
      
       $str_utf8
      
      
       =
      
      
       preg_replace
      
      
       (
      
      
       "/([\x80-\xFF])/e"
      
      
       ,
       
      
      
       "chr(0xC0|ord('\\1')>>6).chr(0x80|ord('\\1')&0x3F)"
      
      
       ,
       
      
      
       $str_iso8859_1
      
      
       );
       
      
      
      
       $str_iso8859_1
      
      
       =
      
      
       preg_replace
      
      
       (
      
      
       "/([\xC2\xC3])([\x80-\xBF])/e"
      
      
       ,
       
      
      
       "chr(ord('\\1')<<6&0xC0|ord('\\2')&0x3F)"
      
      
       ,
       
      
      
       $str_utf8
      
      
       );
       
      
      
       ?>
       
      
      
      HTH,
      
      R. Rajesh Jeba Anbiah
     
    
   
   
    
     zhawari at hotmail dot com
    
   
   
    ¶
   
   
    
     20 years ago
    
   
   
    
     
      Here is how to convert UTF-8 numbers to UCS-2 numbers in hex:
      
      
      
       <?php
       
       
      
      
       function
      
      
       utf8toucs2
      
      
       (
      
      
       $str
      
      
       )
       
       {
       
       for (
      
      
       $i
      
      
       =
      
      
       0
      
      
       ;
      
      
       $i
      
      
       <
      
      
       strlen
      
      
       (
      
      
       $str
      
      
       );
      
      
       $i
      
      
       +=
      
      
       2
      
      
       )
       
       {
       
      
      
       $substring1
      
      
       =
      
      
       $str
      
      
       [
      
      
       $i
      
      
       ].
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       1
      
      
       ];
       
      
      
       $substring2
      
      
       =
      
      
       $str
      
    
      
       [
      
      
       $i
      
      
       +
      
      
       2
      
      
       ].
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       3
      
      
       ];
       
       
       if (
      
      
       hexdec
      
      
       (
      
      
       $substring1
      
      
       ) <
      
      
       127
      
      
       )
       
      
      
       $results
      
      
       =
      
      
       "00"
      
      
       .
      
      
       $str
      
      
       [
      
      
       $i
      
      
       ].
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       1
      
      
       ];
       
       else
       
       {
       
      
      
       $results
      
      
       =
      
      
       dechex
      
      
       ((
      
      
       hexdec
      
      
       (
      
      
       $substring1
      
      
       )-
      
      
       192
      
      
       )*
      
      
       64
      
      
       + (
      
      
       hexdec
      
      
       (
      
      
       $substring2
      
      
       )-
      
      
       128
      
      
       ));
       
       if (
      
      
       $results
      
      
       <
      
      
       1000
      
      
       )
      
      
       $results
      
      
       =
      
      
       "0"
      
      
       .
      
      
       $results
      
      
       ;
       
      
      
       $i
      
      
       +=
      
      
       2
      
      
       ;
       
       }
       
      
      
       $ucs2
      
      
       .=
      
      
       $results
      
      
       ;
       
       }
       
       return
      
      
       $ucs2
      
      
       ;
       
       }
       
       
       echo
      
      
       strtoupper
      
      
       (
      
      
       utf8toucs2
      
      
       (
      
      
       "D985D8B1D8AD"
      
      
       )).
      
      
       "\n"
      
      
       ;
       
       echo
      
      
       strtoupper
      
      
       (
      
      
       utf8toucs2
      
      
       (
      
      
       "456725"
      
      
       )).
      
      
       "\n"
      
      
       ;
       
       
      
      
       ?>
       
      
      
      Input:
      
      D985D8B1D8AD
      
      Output:
      
      06450631062D
      
      
      Input:
      
      456725
      
      Output:
      
      004500670025
     
    
   
   
    
     rasmus at mindplay dot dk
    
   
   
    ¶
   
   
    
     11 years ago
    
   
   
    
     
      Note an important difference between iconv() and mb_convert_encoding() - if you're working with strings, as opposed to files, you most likely want mb_convert_encoding() and not iconv(), because iconv() will add a byte-order marker to the beginning of (for example) a UTF-32 string when converting from e.g. ISO-8859-1, which can throw off all your subsequent calculations and operations on the resulting string.
      
      
      In other words, iconv() appears to be intended for use when converting the contents of files - whereas mb_convert_encoding() is intended for use when juggling strings internally, e.g. strings that aren't being read/written to/from files, but exchanged with some other media.
     
    
   
   
    
     vitek at 4rome dot ru
    
   
   
    ¶
   
   
    
     20 years ago
    
   
   
    
     
      On some systems there may be no such function as iconv(); this is due to the following reason: a constant is defined named `iconv` with the value `libiconv`. So, the string PHP_FUNCTION(iconv) transforms to PHP_FUNCTION(libiconv), and you have to call libiconv() function instead of iconv().
      
      I had seen this on FreeBSD, but I am sure that was a rather special build.
      
      If you'd want not to be dependent on this behaviour, add the following to your script:
      
      
       <?php
       
      
      
       if (!
      
      
       function_exists
      
      
       (
      
      
       'iconv'
      
      
       ) &&
      
      
       function_exists
      
      
       (
      
      
       'libiconv'
      
      
       )) {
       
       function
      
      
       iconv
      
      
       (
      
      
       $input_encoding
      
      
       ,
      
      
       $output_encoding
      
      
       ,
      
      
       $string
      
      
       ) {
       
       return
      
      
       libiconv
      
      
       (
      
      
       $input_encoding
      
      
       ,
      
      
       $output_encoding
      
      
       ,
      
      
       $string
      
      
       );
       
       }
       
       }
       
      
      
       ?>
       
      
      Thanks to tony2001 at phpclub.net for explaining this behaviour.
     
    
   
   
    
     mightye at gmail dot com
    
   
   
    ¶
   
   
    
     17 years ago
    
   
   
    
     
      To strip bogus characters from your input (such as data from an unsanitized or other source which you can't trust to necessarily give you strings encoded according to their advertised encoding set), use the same character set as both the input and the output, with //IGNORE on the output charcter set.
      
      
       <?php
       
      
      
      
       $badUTF8
      
      
       =
      
      
       htmlentities
      
      
       (
      
      
       '†'
      
      
       );
       
       
      
      
      
       $goodUTF8
      
      
       =
      
      
       iconv
      
      
       (
      
      
       "utf-8"
      
      
       ,
      
      
       "utf-8//IGNORE"
      
      
       ,
      
      
       $badUTF8
      
      
       );
       
      
      
       ?>
       
      
      The result of the example does not give you back the dagger character which was the original input (it got lost when htmlentities was misused to encode it incorrectly, though this is common from people not accustomed to dealing with extended character sets), but it does at least give you data which is sane in your target character set.
     
    
   
   
    
     Daniel Klein
    
   
   
    ¶
   
   
    
     9 years ago
    
   
   
    
     
      I just found out today that the Windows and *NIX versions of PHP use different iconv libraries and are not very consistent with each other.
      
      
      Here is a repost of my earlier code that now works on more systems. It converts as much as possible and replaces the rest with question marks:
      
      
      
       <?php
       
      
      
       if (!
      
      
       function_exists
      
      
       (
      
      
       'utf8_to_ascii'
      
      
       )) {
       
      
      
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'en_AU.utf8'
      
      
       );
       
       if (@
      
      
       iconv
      
      
       (
      
      
       "UTF-8"
      
      
       ,
      
      
       "ASCII//IGNORE//TRANSLIT"
      
      
       ,
      
      
       'é'
      
      
       ) ===
      
      
       false
      
      
       ) {
       
      
      
      
       function
      
      
       utf8_to_ascii
      
      
       (
      
      
       $text
      
      
       ) {
       
       return
      
      
       iconv
      
      
       (
      
      
       "UTF-8"
      
      
       ,
      
      
       "ASCII//TRANSLIT"
      
      
       ,
      
      
       $text
      
      
       );
       
       }
       
       }
       
       else {
       
      
      
      
       function
      
      
       utf8_to_ascii
      
      
       (
      
      
       $text
      
      
       ) {
       
       if (
      
      
       is_string
      
      
       (
      
      
       $text
      
      
       )) {
       
      
      
      
       $text
      
      
       =
      
      
       preg_replace_callback
      
      
       (
      
      
       '/\X/u'
      
      
       ,
      
      
       __FUNCTION__
      
      
       ,
      
      
       $text
      
      
       );
       
       }
       
       elseif (
      
      
       is_array
      
      
       (
      
      
       $text
      
      
       ) &&
      
      
       count
      
      
       (
      
      
       $text
      
      
       ) ==
      
      
       1
      
      
       &&
      
      
       is_string
      
      
       (
      
      
       $text
      
      
       [
      
      
       0
      
      
       ])) {
       
      
      
      
       $text
      
      
       =
      
      
       iconv
      
      
       (
      
      
       "UTF-8"
      
      
       ,
      
      
       "ASCII//IGNORE//TRANSLIT"
      
      
       ,
      
      
       $text
      
      
       [
      
      
       0
      
      
       ]);
       
      
      
      
       if (
      
      
       $text
      
      
       ===
      
      
       ''
      
      
       || !
      
      
       is_string
      
      
       (
      
      
       $text
      
      
       )) {
       
      
      
       $text
      
      
       =
      
      
       '?'
      
      
       ;
       
       }
       
       elseif (
      
      
       preg_match
      
      
       (
      
      
       '/\w/'
      
      
       ,
      
      
       $text
      
      
       )) {
      
      
      
       $text
      
      
       =
      
      
       preg_replace
      
      
       (
      
      
       '/\W+/'
      
      
       ,
      
      
       ''
      
      
       ,
      
      
       $text
      
      
       );
      
      
      
       }
       
       }
       
       else {
      
      
      
       $text
      
      
       =
      
      
       ''
      
      
       ;
       
       }
       
       return
      
      
       $text
      
      
       ;
       
       }
       
       }
       
       }
      
     
    
   
   
    
     anyean at gmail dot com
    
   
   
    ¶
   
   
    
     20 years ago
    
   
   
    
     
      
       <?php
       
      
      
      
       function
      
      
       unescape
      
      
       (
      
      
       $str
      
      
       ) {
       
      
      
       $str
      
      
       =
      
      
       rawurldecode
      
      
       (
      
      
       $str
      
      
       );
       
      
      
       preg_match_all
      
      
       (
      
      
       "/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U"
      
      
       ,
      
      
       $str
      
      
       ,
      
      
       $r
      
      
       );
       
      
      
       $ar
      
      
       =
      
      
       $r
      
      
       [
      
      
       0
      
      
       ];
       
      
      
       print_r
      
      
       (
      
      
       $ar
      
      
       );
       
       foreach(
      
      
       $ar
      
      
       as
      
      
       $k
      
      
       =>
      
      
       $v
      
      
       ) {
       
       if(
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,
      
      
       0
      
      
       ,
      
      
       2
      
      
       ) ==
      
      
       "%u"
      
      
       )
       
      
      
       $ar
      
      
       [
      
      
       $k
      
      
       ] =
      
      
       iconv
      
      
       (
      
      
       "UCS-2"
      
      
       ,
      
      
       "UTF-8"
      
      
       ,
      
      
       pack
      
      
       (
      
      
       "H4"
      
      
       ,
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,-
      
      
       4
      
      
       )));
       
       elseif(
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,
      
      
       0
      
      
       ,
      
      
       3
      
      
       ) ==
      
      
       "&#x"
      
      
       )
       
      
      
       $ar
      
      
       [
      
      
       $k
      
      
       ] =
      
      
       iconv
      
      
       (
      
      
       "UCS-2"
      
      
       ,
      
      
       "UTF-8"
      
      
       ,
      
      
       pack
      
      
       (
      
      
       "H4"
      
      
       ,
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,
      
      
       3
      
      
       ,-
      
      
       1
      
      
       )));
       
       elseif(
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,
      
      
       0
      
      
       ,
      
      
       2
      
      
       ) ==
      
      
       "&#"
      
      
       ) {
       
       echo
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,
      
      
       2
      
      
       ,-
      
      
       1
      
      
       ).
      
      
       "<br>"
      
      
       ;
       
      
      
       $ar
      
      
       [
      
      
       $k
      
      
       ] =
      
      
       iconv
      
      
       (
      
      
       "UCS-2"
      
      
       ,
      
      
       "UTF-8"
      
      
       ,
      
      
       pack
      
      
       (
      
      
       "n"
      
      
       ,
      
      
       substr
      
      
       (
      
      
       $v
      
      
       ,
      
      
       2
      
      
       ,-
      
      
       1
      
      
       )));
       
       }
       
       }
       
       return
      
      
       join
      
      
       (
      
      
       ""
      
      
       ,
      
      
       $ar
      
      
       );
       
       }
       
      
      
       ?>
      
     
    
   
   
    
     phpmanualspam at netebb dot com
    
   
   
    ¶
   
   
    
     15 years ago
    
   
   
    
     
      mirek code, dated 16-May-2008 10:17, added the characters `^~'" to the output.
      
      This function will strip out these extra characters:
      
      
       <?php
       
       setlocale
      
      
       (
      
      
       LC_ALL
      
      
       ,
      
      
       'en_US.UTF8'
      
      
       );
       
       function
      
      
       clearUTF
      
      
       (
      
      
       $s
      
      
       )
       
       {
       
      
      
       $r
      
      
       =
      
      
       ''
      
      
       ;
       
      
      
       $s1
      
      
       = @
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       $s
      
      
       );
       
      
      
       $j
      
      
       =
      
      
       0
      
      
       ;
       
       for (
      
      
       $i
      
      
       =
      
      
       0
      
      
       ;
      
      
       $i
      
      
       <
      
      
       strlen
      
      
       (
      
      
       $s1
      
      
       );
      
      
       $i
      
      
       ++) {
       
      
      
       $ch1
      
      
       =
      
      
       $s1
      
      
       [
      
      
       $i
      
      
       ];
       
      
      
       $ch2
      
      
       = @
      
      
       mb_substr
      
      
       (
      
      
       $s
      
      
       ,
      
      
       $j
      
      
       ++,
      
      
       1
      
      
       ,
      
      
       'UTF-8'
      
      
       );
       
       if (
      
      
       strstr
      
      
       (
      
      
       '`^~\'"'
      
      
       ,
      
      
       $ch1
      
      
       ) !==
      
      
       false
      
      
       ) {
       
       if (
      
      
       $ch1
      
      
       <>
      
      
       $ch2
      
      
       ) {
       
       --
      
      
       $j
      
      
       ;
       
       continue;
       
       }
       
       }
       
      
      
       $r
      
      
       .= (
      
      
       $ch1
      
      
       ==
      
      
       '?'
      
      
       ) ?
      
      
       $ch2
      
      
       :
      
      
       $ch1
      
      
       ;
       
       }
       
       return
      
      
       $r
      
      
       ;
       
       }
       
      
      
       ?>
      
     
    
   
   
    
     Daniel Klein
    
   
   
    ¶
   
   
    
     12 years ago
    
   
   
    
     
      You can use 'CP1252' instead of 'Windows-1252':
      
      
       <?php
       
      
      
      
       $result
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'Windows-1252'
      
      
       ,
      
      
       'UTF-8'
      
      
       ,
      
      
       $string
      
      
       );
       
      
      
       $result
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'CP1252'
      
      
       ,
      
      
       'UTF-8'
      
      
       ,
      
      
       $string
      
      
       );
       
      
      
       ?>
       
      
      Note: The following code points are not valid in CP1252 and will cause errors.
      
      129 (0x81)
      
      141 (0x8D)
      
      143 (0x8F)
      
      144 (0x90)
      
      157 (0x9D)
      
      Use the following instead:
      
      
       <?php
       
      
      
      
       $result
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'CP1252'
      
      
       ,
      
      
       'UTF-8//IGNORE'
      
      
       ,
      
      
       $string
      
      
       );
       
      
      
       ?>
      
     
    
   
   
    
     Locoluis
    
   
   
    ¶
   
   
    
     18 years ago
    
   
   
    
     
      The following are Microsoft encodings that are based on ISO-8859 but with the addition of those stupid control characters.
      
      
      CP1250 is Eastern European (not ISO-8859-2)
      
      CP1251 is Cyrillic (not ISO-8859-5)
      
      CP1252 is Western European (not ISO-8859-1)
      
      CP1253 is Greek (not ISO-8859-7)
      
      CP1254 is Turkish (not ISO-8859-9)
      
      CP1255 is Hebrew (not ISO-8859-8)
      
      CP1256 is Arabic (not ISO-8859-6)
      
      CP1257 is Baltic (not ISO-8859-4)
      
      
      If you know you're getting input from a Windows machine with those encodings, use one of these as a parameter to iconv.
     
    
   
   
    
     Andries Seutens
    
   
   
    ¶
   
   
    
     15 years ago
    
   
   
    
     
      When doing transliteration, you have to make sure that your LC_COLLATE is properly set, otherwise the default POSIX will be used.
      
      
      To transform "rené" into "rene" we could use the following code snippet:
      
      
      
       <?php
       
       
       setlocale
      
      
       (
      
      
       LC_CTYPE
      
      
       ,
      
      
       'nl_BE.utf8'
      
      
       );
       
       
      
      
       $string
      
      
       =
      
      
       'rené'
      
      
       ;
       
      
      
       $string
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       $string
      
      
       );
       
       
       echo
      
      
       $string
      
      
       ;
      
      
      
       ?>
      
     
    
   
   
    
     vb (at) bertola.eu
    
   
   
    ¶
   
   
    
     15 years ago
    
   
   
    
     
      On my system, according to tests, and also as reported by other people elsewhere, you can combine TRANSLIT and IGNORE only by appending
      
      
      //IGNORE//TRANSLIT
      
      
      strictly in that order, but NOT by appending //TRANSLIT//IGNORE, which would lead to //IGNORE being ignored ( :) ).
      
      
      Anyway, it's hard to understand how one could devise a system of passing options that does not allow to couple both options in a neat manner, and also to understand why the default behaviour should be the less useful and most dangerous one (throwing away most of your data at the first unexpected character). Software design FAIL :-/
     
    
   
   
    
     berserk220 at mail dot ru
    
   
   
    ¶
   
   
    
     17 years ago
    
   
   
    
     
      So, as iconv() does not always work correctly, in most cases, much easier to use htmlentities().
      
      Example:
      
       <?php $content
      
      
       =
      
      
       htmlentities
      
      
       (
      
      
       file_get_contents
      
      
       (
      
      
       "incoming.txt"
      
      
       ),
      
      
       ENT_QUOTES
      
      
       ,
      
      
       "Windows-1252"
      
      
       );
      
      
       file_put_contents
      
      
       (
      
      
       "outbound.txt"
      
      
       ,
      
      
       html_entity_decode
      
      
       (
      
      
       $content
      
      
       ,
      
      
       ENT_QUOTES
      
      
       ,
      
      
       "utf-8"
      
      
       ));
      
      
       ?>
      
     
    
   
   
    
     mirek at burkon dot org
    
   
   
    ¶
   
   
    
     17 years ago
    
   
   
    
     
      If you need to strip as many national characters from UTF-8 as possible and keep the rest of input unchanged (i.e. convert whatever can be converted to ASCII and leave the rest), you can do it like this:
      
      
      
       <?php
       
       setlocale
      
      
       (
      
      
       LC_ALL
      
      
       ,
      
      
       'en_US.UTF8'
      
      
       );
       
       
       function
      
      
       clearUTF
      
      
       (
      
      
       $s
      
      
       )
       
       {
       
      
      
       $r
      
      
       =
      
      
       ''
      
      
       ;
       
      
      
       $s1
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'UTF-8'
      
      
       ,
      
      
       'ASCII//TRANSLIT'
      
      
       ,
      
      
       $s
      
      
       );
       
       for (
      
      
       $i
      
      
       =
      
      
       0
      
      
       ;
      
      
       $i
      
      
       <
      
      
       strlen
      
      
       (
      
      
       $s1
      
      
       );
      
      
       $i
      
      
       ++)
       
       {
       
      
      
       $ch1
      
      
       =
      
      
       $s1
      
      
       [
      
      
       $i
      
      
       ];
       
      
      
       $ch2
      
      
       =
      
      
       mb_substr
      
      
       (
      
      
       $s
      
      
       ,
      
      
       $i
      
      
       ,
      
      
       1
      
      
       );
       
       
      
      
       $r
      
      
       .=
      
      
       $ch1
      
      
       ==
      
      
       '?'
      
      
       ?
      
      
       $ch2
      
      
       :
      
      
       $ch1
      
      
       ;
       
       }
       
       return
      
      
       $r
      
      
       ;
       
       }
       
       
       echo
      
      
       clearUTF
      
      
       (
      
      
       'Šíleně žluťoučký Vašek úpěl olol! This will remain untranslated: ᾡᾧῘઍિ૮'
      
      
       );
       
      
      
      
       ?>
       
      
      
      Just remember you HAVE TO set locale to some unicode encoding to make iconv handle //TRANSLIT correctly!
     
    
   
   
    
     aissam at yahoo dot com
    
   
   
    ¶
   
   
    
     20 years ago
    
   
   
    
     
      For those who have troubles in displaying UCS-2 data on browser, here's a simple function that convert ucs2 to html unicode entities :
      
      
      
       <?php
       
       
      
      
       function
      
      
       ucs2html
      
      
       (
      
      
       $str
      
      
       ) {
       
      
      
       $str
      
      
       =
      
      
       trim
      
      
       (
      
      
       $str
      
      
       );
      
      
      
       $len
      
      
       =
      
      
       strlen
      
      
       (
      
      
       $str
      
      
       );
       
      
      
       $html
      
      
       =
      
      
       ''
      
      
       ;
       
       for(
      
      
       $i
      
      
       =
      
      
       0
      
      
       ;
      
      
       $i
      
      
       <
      
      
       $len
      
      
       ;
      
      
       $i
      
      
       +=
      
      
       2
      
      
       )
       
      
      
       $html
      
      
       .=
      
      
       '&#'
      
      
       .
      
      
       hexdec
      
      
       (
      
      
       dechex
      
      
       (
      
      
       ord
      
      
       (
      
      
       $str
      
      
       [
      
      
       $i
      
      
       +
      
      
       1
      
      
       ])).
       
      
      
       sprintf
      
      
       (
      
      
       "%02s"
      
      
       ,
      
      
       dechex
      
      
       (
      
      
       ord
      
      
       (
      
      
       $str
      
      
       [
      
      
       $i
      
      
       ])))).
      
      
       ';'
      
      
       ;
       
       return(
      
      
       $html
      
      
       );
       
       }
       
      
      
       ?>
      
     
    
   
   
    
     martin at front of mind dot co dot uk
    
   
   
    ¶
   
   
    
     16 years ago
    
   
   
    
     
      For transcoding values in an Excel generated CSV the following seems to work:
      
      
      
       <?php
       
       $value
      
      
       =
      
      
       iconv
      
      
       (
      
      
       'Windows-1252'
      
      
       ,
      
      
       'UTF-8//TRANSLIT'
      
      
       ,
      
      
       $value
      
      
       );
       
      
      
       ?>