Skip to content

Instantly share code, notes, and snippets.

@tistre tistre/charcheck.php
Created May 29, 2015

Embed
What would you like to do?
Check files for Unicode U+00A0 No-Break Space
<?php
// Find invisible non-breaking spaces in source code
// which can cause errors in PHP and JavaScript.
//
// see: http://www.strehle.de/tim/weblog/archives/2013/02/26/1563
//
// Usage:
// php charcheck.php path/to/*.php
// or:
// find path/to -name '*.php' | php charcheck.php -
function checkFile($filename)
{
// "| ": space after pipe (c2 a0)
$check = chr(194) . chr(160);
$lines = file($filename);
$matches = array();
foreach ($lines as $lineno => $line)
{
if (strpos($line, $check) !== false)
{
$matches[ ] = ($lineno + 1);
}
}
if (count($matches) > 0)
{
printf("%s %s\n", $filename, implode(',', $matches));
}
}
if ($argv[ 1 ] === '-')
{
while (! feof(STDIN))
{
$filename = trim(fgets(STDIN));
if ($filename !== '')
{
checkFile($filename);
}
}
}
else
{
for ($i = 1; $i < $argc; $i++)
{
checkFile($argv[ $i ]);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.