将php数据发送到textarea - 编码问题

I've using two textareas and a button. The idea is when the button is pressed all the information on textarea1 will be send to the php function reverse them and then return the result on textarea2 (I am using ajax as well so I don't have to reload the website).

Now the issue is, although this works with properly there is an issue with special symbols since they show up as � when they come back in textarea2 (not all of them though) so I'm assuming there is an issue with the encoding somewhere.

This is the simple php code that returns the result to textarea2

<?php
$data = rawurldecode($_GET["data"]);

//mb_internal_encoding("UTF-8");
//mb_http_output( "UTF-8" );
// ob_start("mb_output_handler");

echo strrev($data);
?>

As you can see I tried already to set the internal encoding to UTF-8, also I've tried encoding the data before they get send to php and decode them in the php function but it had the same effect.

strrev() is going to reverse the sequence of bytes which isn't good news for variable byte length encodings such as UTF-8. Characters that can be represented with one byte (such as ASCII) in UTF-8 are safe, but ones which are not are going to be mangled.

You can use a regular expression with the Unicode flag enabled to get around this limitation of strrev().

// `u` flag so `.` match Unicode characters and `s` flag so `.` matches `
`.
preg_match_all('/./us', $str, $matches);
// Reverse the array of matches, and then join the characters back together.
$str = implode(array_reverse($matches[0]));