var_dump reports wrong string length

I have a php script that checks whether array contains a certain value, but it doesn’t seem to be working. To debug this I am using var_dump to print the contents of the array and the value.

//Check the node we want to insert doesn't already exist
        if( isset($siblings[$this->_selectedCat]) )
        {
            //Return the correct node id
            return $siblings[$this->_selectedCat]['id'];
        }
        //Otherwise insert it
        else
        {
            var_dump($siblings);
            var_dump($this->_selectedCat); exit();
        }

The contents of the var_dump show that the value does exist in the array, so that doesn’t explain why PHP thinks it isn’t in the array. However, I noticed that the value seemed to have a longer length than it’s actual length

string(9) "places"

The only info I’ve been able to find about this is where the string actually contains some HTML code, which the browser converts to elements or entities. However. PHP outputs from my script to a text file, which I am viewing in a text editor, not a browser, so this can’t be the case here.

Could be a problem due to character encoding?

Any suggestions appreciated.

Dave

Without seeing the full dumps, it’s hard to say. Which dump is that?

There is a “Byte order mark” preceding the places in that string, which accounts for the extra length.

If you wish to know the number of characters in a string, use the strlen() function

Thanks, that was the problem.

The string I was comparing had originally come from a text file saved with Windows notepad, which includes a BOM when you save as UTF-8. In my case I am fixing my problem by altering my data (using a text editor that allows saving as UTF-8 without a BOM), but I also found this PHP based solution if anyone else has a similar problem in the future:

Detecting UTF BOM – byte order mark

You’re welcome. Have a nice weekend. (:

Here’s a similar script I hacked together to scan an entire directory (recursively) for files with BOMs in them:


<?php

// utility file to scan for PHP files containing a Byte Order Mark

$directory = new RecursiveDirectoryIterator(realpath(__DIR__).'/../../');
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\\.php$/i', RecursiveRegexIterator::GET_MATCH);

foreach($regex as $match) {
    $file = $match[0];
    $contents = file_get_contents($file);
    if(substr($contents, 0, 3) == pack("CCC",0xef,0xbb,0xbf)) {
        echo $file . "\
";
    }
}

?>

It doesn’t actually change anything, but you could use it to scan an entire project to see which PHP files have BOMs (you’ll probably need to update the path in the first directoryiterator though) :slight_smile: