The Developer Day | Staying Curious

TAG | json

Mar/13

7

Cleaning Invalid UTF-8 characters in PHP

I ran into an ugly issue having to discard invalid UTF-8 characters from a string before I pass it to json_decode() as otherwise it fails decoding it. First I’ve discovered that it’s possible to ignore invalid UTF-8 characters using:

iconv(“UTF-8″, “UTF-8//IGNORE”, $text)

However turns out this has been broken for ages and using //IGNORE produces an E_NOTICE. Luckily I found a comment which suggests a workaround:

ini_set(‘mbstring.substitute_character’, “none”);
$text = mb_convert_encoding($text, ‘UTF-8′, ‘UTF-8′);

This however was not enough. Because I was getting some characters that were non printable UTF-8 characters json_decode was failing on them as well. To work around this I’ve used:

$text = preg_replace(‘/[^\pL\pN\pP\pS\pZ\pM]/u’, ”, $text);

This will remove new lines as well which is fine for me. You can also try a removing non-printable byte sequences.

, , , Hide

Aug/07

6

JavaScript Associative Arrays & Iterations

Today I was working with a little piece of JavaScript code and writing something like this:

var tmp = new Array();
tmp['id']= '123';
tmp['name'] = 'bla bla';

And then i tried to iterate over the array like this:

for (var i in tmp) { alert(i); };

If you haven’t done a lot of JavaScript you would probably say that the above iteration should output ‘id’, ‘name’. Surprisingly you may be wrong. If you are using any code that extends the Array prototype with attributes or functions you will see them in the output. This may happen if you are using the prototype framework or The open source code of a JSON parser and JSON stringifier. All you’re doing is setting properties on an object. After reading a few blog posts and comments on this I found yet another issue that ideally I shouldn’t be using new Array() and using new Object(). That sounds kind of true because JavaScript does not support associative arrays and you could do the same on any object. Date, RegExp, Boolean or any other. Actually using Object solved the prototype framework problem, because prototype framework does not extend the Object prototype since 1.4 but the JSON stringifier does.

After googling a while I found out a little tricksy that may mostly work with this problem. You could do something like this:

for (var i in obj) {
if (!obj.propertyIsEnumerable(i)) continue;
foo(obj[i]);
}

This only works if properties are enumerable. That may not always save you especially if you are using any 3rd party software that breaks this, but it certainly helps you while solving the prototype framework or JSON stringifier puzzle.

, , , , Hide

Find it!

Theme Design by devolux.org