3

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.

1
  • 3
    Looks like it's something to do with the encoding. It's Reykjavík, not Reykjavik. Note the special character í Commented Sep 3, 2012 at 12:36

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

I've got Notice: Use of undefined constant UTF - assumed 'UTF' ; I think that PHP is seeing UTF-8 as "UTF" minus 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.