I'm having trouble decoding Greek text when using ajaxed infinite scrolling. It's the first time I'm dealing with non-English data, but as far as I understand every single Greek character needs to be escaped, because otherwise Ajax breaks trying so send the characters.
I make it Ajax-friendly by escaping it with this (PHP):
function utf8ize($d) { // Encoding workaround
if(is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} elseif (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}
so this
Το γράμμα άλφα (ἄλφα) είναι το πρώτο γράμμα του ελληνικού αλφαβήτου.
becomes this:
Το γÏάμμα άλφα (ἄλφα) είναι το Ï€Ïώτο γÏάμμα του ÎµÎ»Î»Î·Î½Î¹ÎºÎ¿Ï Î±Î»Ï†Î±Î²Î®Ï„Î¿Ï….
which is how the text looks raw on my UK-locale database. But now I am not sure how to convert it back to Greek on the front-end.
Normally I would successfully decode non Basic Latin words like café, fiancé, façade using PHP's utf8_encode at back-end and then jQuery's decodeURIComponent on front-end, but with Greek this error comes up
URIError: URI malformed
Is there an in-built jQuery function to convert utf-8 into another format that supports Greek at front-end?
This is how it looks on default load:
And this is what happens when I try to inject the same text via Ajax


I figured out the problem thanks to @Hackerman and @HarryPehkonen comments.
The original problem was that the Greek text also had hyperlinks with mixed characters.
For example Greek links have Latin-based domain names, but use Greek for semantic slugs.
Which look Greek in the URL bar, but are actually already URL encoded and look like this when copy-pasted into text editor.
And it's the last part that seemed to break things.
So in sample input
Trying to utf8_encode and then json_encode a string which already contains URL encoded sections resulted in the string being neither when decoded back at front-end.
Modifying my utf8ize() function to do an extra iconv('UTF-8', 'UTF-8', $d) fixed the problem.