Skip to content

Instantly share code, notes, and snippets.

@tistre
Created May 29, 2015 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tistre/40277b47155b7f5a678c to your computer and use it in GitHub Desktop.
Save tistre/40277b47155b7f5a678c to your computer and use it in GitHub Desktop.
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