I have a string with some international characters. The information containing these characters is pulled onto the page using AJAX. Here is the weird set of circumstances:
If I load the information using transport.reponseText (where transport is the AJAX object) everything loads fine.
If I want to use json_encode there is an issue. It will cut off the string and stop my JSON output right there.
This does not happen with this code:
$response['html'] = $description."<br />";
echo json_encode($response);
But if I truncate longer words it will:
function truncate($txt, $max, $sub) {
$txtRay = explode(" ", $txt);
$new_txt = '';
foreach ($txtRay as $key => $word) {
$len = strlen($word);
if ($len > $max) {
$start = mb_substr($word, 0, ($max-$sub));
$end = mb_substr($word, ($len-$sub));
$word = $start."...".$end;
}
$new_txt .= ' '.$word;
}
$new_txt = trim($new_txt);
return $new_txt;
}
$response['html'] = truncate($description, 35, 6)."<br /><br />";
echo json_encode($response);
As you can see I am using mb_substr to be extra safe. However when I print out the json_encoded string AFTER running it through that function, it cuts off shortly after the letter.