PHP echo中的奇怪字符导致无效的JSON

I am using echo in php to return a database request. When trying to decode the result in Flash I get errors telling me of unexpected characters. When I looked at the output in the browser it looked fine. But when I copied the contents (Select All -> Copy) and then ran the text through a script in php I saw a lot of strange characters. This explains why the JSON decoding fails, but how do I get rid of the strange characters?

Test script to check the string

    $string = str_split($string);
    echo '<pre>';
    print_r($string);
    echo '<pre>';

Result:

    [0] => ï
        [1] => »
        [2] => ¿
        [3] => ï
        [4] => »
        [5] => ¿
        [6] => ï
        [7] => »
        [8] => ¿
...

I found the solution after reading this answer. The problem was that my PHP files were saved with UTF-8 encoding with BOM. Saving the files with UTF-8 encoding solved my problem.

Ensure the server is replying with the correct content-type header:

Content-type: application/json; charset=utf-8