views:

50

answers:

4

I am working on a CakePHP site saved in MacRoman char encoding. I want to change all the files to UTF-8 for internationalisation. For all the other files in the site this works fine. However, in the core.php file there is a security salt, which is a string with special characters ("!:* etc.). When I save this file as UTF-8 the salt gets corrupted. I can roll this back with git, but it's an annoyance.

Does anyone know how I can convert the string from MacRoman to UTF-8?

+1  A: 

Have you tried mb-convert-encoding ?

Think it would be:

$str = mb_convert_encoding($str, "macintosh", "UTF-8");
seengee
A: 

Just curious, have you tried copying the salt, saving as UTF-8 and then pasting the salt back in place and saving again?

Kristoffer S Hansen
Yes, I should have mentioned that I did try that but it didn't help
+3  A: 

You don't give enough information to confirm this, but I guess the salt is used in its binary form. In that case, changing the encoding of the file will corrupt the salt if this binary stream is changed, even if the characters are correctly converted.

Since the first 128 characters are similar in UTF-8 and Mac OS Roman, you don't have to worry if the salt is written using only these characters.

Let's say the salt is somewhere:

$salt = "a!c‡Œ";

You could write instead:

$salt = "a!c\xE0\xCE";

You could map all to their hexadecimal representation, as it might be easier to automate:

$salt = "\x61\x21\x63\xE0\xCE";

See the table here.

The following snippet can automate this conversion:

$res = "";
foreach (str_split($salt) as $c) {
    $res .= "\\x".dechex(ord($c));
}
echo $res;
Artefacto
+1  A: 

Thanks for the input, pointed me in the right direction. The solution is:

$salt = iconv('UTF-8', 'macintosh', $string);