0

I'm trying to implement Caesar cipher using PHP, but my scrips's got problem with polish signs. I'm using UTF-8 charset but signs like "ĄŚĆŃ" are displayed like '�' anyway.

Here is my code:

$text='MYCODE'; //code to crypt
$alphabet="AĄBCĆDEĘFGHIJKLŁMNŃOÓPRSŚTUWYZŹŻ";

$n=strlen($text);
$new_text='';
for ($i=0; $i<$n; $i++) 
    $new_text.=$alphabet[(strpos($alphabet,$text[$i])+5)%32];

echo $new_text;
echo "</br>";

and that's the result of it: '�E�SGH'

When I work with english alphabet it works correctly.

  • 1
    you never bothered sending an appropriate mime/content-type header to the client, so it's free to interpret that utf text however it wants. you have to TELL the client what you're sending. – Marc B Apr 18 '16 at 21:26
  • Ty for answer. So what can be appropriate mime/content-type header for this? If I write "echo $alphabet;" – user3085804 Apr 18 '16 at 21:32
  • that's because php isn't really unicode aware. when you do `$alpha[26]` you're telling to output the 26th **BYTE** in that string, not the 26th character. that means you're splitting up a multi-byte sequence and sending out only one of its bytes, which corrupts the text. That's why there's the mb_*() functions, which ARE multibyte aware. – Marc B Apr 18 '16 at 21:34
  • read this http://www.w3schools.com/html/html_charset.asp – Kamil Karkus Apr 18 '16 at 21:34

0 Answers0