Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null values in json_encode with a generated PHP array

I've been looking over the simple html dom for PHP and it works very well. The problem is when I try to add the returned values to a PHP array.

I have an example where you can see the result http://fae.ctrl.fo/plane_script/ and the script below is what gives the data from that link.

<?PHP

include('simple_html_dom.php');

$resultSet = array();
$html = file_get_html('http://212.55.50.147');
foreach($html->find('tr[class^=FlightScheduleItem]') as $tr)
    {
        $row = array();
        foreach($tr->find('td') as $td) 
        {
            echo $td->innertext." ";
            $row[] = $td->innertext; 
        }
        $resultSet[] = $row;
    }

    echo "<br/><br/><br/>";

echo $_GET['callback'] . '(' .json_encode($resultSet) . ')';
?>

As you can see I'm printing all the values I find in the td tag and I'm saving them in an array in the same loop. If you check the output you'll see that "Reykjavík" is not found in the array.

Do you have any idea why?

later edit:

If I use the print_r function from PHP I can find the word in my array. This means that json_encode transforms Reykjavík to null.

Do you know why or an alternative?

Thanks

later edit:

Thanks to your help I modified this line:

$row[] = htmlentities($td->innertext, UTF-8); 

... and now it works.

Thank you.

like image 548
alurosu Avatar asked Dec 04 '25 12:12

alurosu


1 Answers

It seems json_encode expects UTF-8 input, but the default on PHP is ISO-8859-1.

You should be able to do this:

$resultSet[] = htmlentities($row, UTF-8)

This should encode the latin character as UTF-8, allowing it to be passed through (encoded) in the json_encode function.

Solution found on bugs.php.net

like image 71
Nick Avatar answered Dec 06 '25 02:12

Nick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!