
Si consigues descubrir el archivo que contiene estos caracteres no es difícil (aunque sí aburrido) eliminarlos. Pero por suerte PHP puede leer los caracteres BOM y, como no, eliminarlos con la función adecuada. Aquí os dejo un script PHP que recorrerá todos los archivos que encuentre en el directorio actual y sus subdirectorios, abrirá estos archivos en busca de caracteres BOM y los eliminará actualizando el contenido del archivo sin los BOM.
Abre tu editor favorito, pega este código y guárdalo en un archivo PHP. Súbelo a tu servidor y ejecútalo. Tan simple y tan efectivo!!!
<?php
// Fuente: https://gist.github.com/mdoelker/9269378
// Strips BOM from all php files in the current dir and subdirs
$files = stripUtf8BomFromFiles(".", "*.php");
/**
* Remove BOM from a single file
*
* @param string $filename
* @return bool
*/
function stripUtf8BomFromFile($filename) {
$bom = pack('CCC', 0xEF, 0xBB, 0xBF);
$file = @fopen($filename, "r");
$hasBOM = fread($file, 3) === $bom;
fclose($file);
if ($hasBOM) {
$contents = file_get_contents($filename);
file_put_contents($filename, substr($contents, 3));
}
return $hasBOM;
}
/**
* Removes BOM from all matching files in a directory
*
* @param string $path Path to search in
* @param string $exclude Dirs to exclude from search
* @param string $file_pattern File name pattern, e.g. *.php
* @param bool $recursive Whether to do a recursive search
* @param bool $verbose Whether to print fixed files
* @return array Path names of all fixed files
*/
function stripUtf8BomFromFiles($path, $file_pattern = "*", $exclude = ".|..",
$recursive = true, $verbose = true) {
if (false !== $file_pattern && !function_exists('fnmatch')) {
die(
'Required function fnmatch is not available. Please upgrade to ' .
'PHP >= 5.3.0 on Windows or disable pattern matching by ' .
'passing in false.'
);
}
$path = rtrim($path, "/")."/";
$dir = opendir($path);
$exclude_array = explode("|", $exclude);
$files = array();
while (false !== ($filename = readdir($dir))) {
if (!in_array(strtolower($filename), $exclude_array)) {
if (is_dir($path.$filename."/") && $recursive) {
$result[] = stripUtf8BomFromFiles(
$path.$filename."/",
$file_pattern,
$exclude,
$recursive,
$verbose
);
} elseif (false === $file_pattern || fnmatch($file_pattern, $filename)) {
if (stripUtf8BomFromFile($path.$filename)) {
if ($verbose) { printf("fixed %sn", $path.$filename); }
$files[] = $path.$filename;
}
}
}
}
return $files;
}
Descarga el script para eliminar caracteres BOM
Comentarios