• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1368
  • Last Modified:

using utf8_encode() on text that is already encoded

What happens if I use utf8_encode() on text that is already in a UTF-8 encoding?  If problems could be caused, how could they be avoided?
0
hankknight
Asked:
hankknight
  • 2
2 Solutions
 
Ray PaseurCommented:
The man page says <QUOTE>Encodes an ISO-8859-1 string to UTF-8</QUOTE> so I expect that you would mung the data if you did this.  However the user-contributed notes contain a couple of examples on how to detect if a string is UTF-8.  You might use those in advance to know what strings to avoid.

http://us3.php.net/manual/en/function.utf8-encode.php

HTH, ~Ray
0
 
absxCommented:
Hi,

It's possible to double-encode your text this way.

<?PHP
$var = "Hyvää päivää";
for($i=0;$i<4;$i++) {
$var = utf8_encode($var);
echo $var."<br>\n";
}
?>

Returns:
Hyvää päivää!
Hyvää päivää!
Hyvää päivää!
Hyvää päivää!

(Hope this looks as it should in EE).


There is no sure-fire way to salvage any character encoding. The only way you can avoid problems is by planning ahead and knowing what's the encoding for each string you handle. I'd say the most common problem is to double-encode your user-input UTF-8 strings by forgetting to initialize the database connection in UTF-8. For example in MySQL, this would be done by issuing the command "SET NAMES UTF8;".
0
 
Ray PaseurCommented:

<?php // RAY_utf8_encode.php
 
echo "<pre>";
 
// SOME TEST DATA
$var = "Hyvää päivää";
$arr[] = $var;
 
// ENCODE IT TO DEATH
for($i=0;$i<4;$i++)
{
   $var = utf8_encode($var);
   $arr[] = $var;
}
 
// PRESENT
var_dump($arr);
 
// DECODE
echo "\n";
foreach ($arr as $pointer => $thing)
{
  $thing = utf8_decode($thing);
  var_dump($thing);
  $arr[$pointer] = $thing;
}
 
// DECODE AGAIN
echo "\n";
foreach ($arr as $pointer => $thing)
{
  $thing = utf8_decode($thing);
  var_dump($thing);
  $arr[$pointer] = $thing;
}
 
// DECODE AGAIN
echo "\n";
foreach ($arr as $pointer => $thing)
{
  $thing = utf8_decode($thing);
  var_dump($thing);
  $arr[$pointer] = $thing;
}

Open in new window

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now