- Detalles
- Por Juan Padial
- En Programación
- Visto: 1752
Los caracteres BOM (Byte Order Mark) pueden causar muchos problemas en archivos php y xml, como el error Cannot modify header information - headers already sent. Estos caracteres son a menudo introducidos cuándo se trabaja en algún editor en Windows, guardamos el archivo en codificación UTF-8 y son introducidos para "decirle" al sistema que se ha codificado el archivo en UTF-8.
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, con la función adecuada eliminarlos.
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 de código, pega este código y guardalo en un archivo PHP. Súbelo a tu servidor y abrelo en tu navegador de internet. Tan simple y tan efectivo!!!
Fuente: http://emrg.me/6j
<?php
// Tell me the root folder path.
// You can also try this one
// $HOME = $_SERVER["DOCUMENT_ROOT"];
// Or this
// dirname(__FILE__)
$HOME = dirname(__FILE__);
// Is this a Windows host ? If it is, change this line to $WIN = 1;
$WIN = 0;
// That's all I need
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF8 BOM FINDER and REMOVER</title>
<style>
body { font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: #FFF; color: #000; }
.FOUND { color: #F30; font-size: 14px; font-weight: bold; }
</style>
</head>
<body>
<?php
$BOMBED = array();
RecursiveFolder($HOME);
echo '<h2>These files had UTF8 BOM, but i cleaned them:</h2><p class="FOUND">';
foreach ($BOMBED as $utf) { echo $utf ."<br />\n"; }
echo '</p>';
// Recursive finder
function RecursiveFolder($sHOME) {
global $BOMBED, $WIN;
$win32 = ($WIN == 1) ? "\\" : "/";
$folder = dir($sHOME);
$foundfolders = array();
while ($file = $folder->read()) {
if($file != "." and $file != "..") {
if(filetype($sHOME . $win32 . $file) == "dir"){
$foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
} else {
$content = file_get_contents($sHOME . $win32 . $file);
$BOM = SearchBOM($content);
if ($BOM) {
$BOMBED[count($BOMBED)] = $sHOME . $win32 . $file;
// Remove first three chars from the file
$content = substr($content,3);
// Write to file
file_put_contents($sHOME . $win32 . $file, $content);
}
}
}
}
$folder->close();
if(count($foundfolders) > 0) {
foreach ($foundfolders as $folder) {
RecursiveFolder($folder, $win32);
}
}
}
// Searching for BOM in files
function SearchBOM($string) {
if(substr($string,0,3) == pack("CCC",0xef,0xbb,0xbf)) return true;
return false;
}
?>
</body>
</html> Creado el 25 11 2011 Actualizado el 02 10 2012
