您的位置:首页 > 编程语言 > PHP开发

【整理】解决php输出时出现多余的空格或者换行

2011-09-28 20:13 489 查看
由于某度众所周知的举动,让我搬离写了5年的渣度空间,准备把技术性的文章定在CSDN了。这些都是文章备份。勿怪。。

1.要查清自己本身有没有echo 或者exit空格或者换行,

2.一定要保证php文件里<?php ?>标签外没有多余的回车,换行。

3.这些都排查了之后,如果是utf8编码的文件,还会输出一个多余的空格回车。做xml传输时经常死在这多余的空白上面。这是由于BOM文件头造成的。可以使用ultraedit打开文件,另存为无BOM的文件即可。如果文件太多,可以使用以下文件保存到根目录执行一次:

<?php

//remove the utf-8 boms

//by magicbug at gmail dot com

if (isset($_GET['dir'])){

//config the basedir

$basedir=$_GET['dir'];

}else{

$basedir = '.';

}

$auto = 1;

checkdir($basedir);

function checkdir($basedir){

if ($dh = opendir($basedir)) {

while (($file = readdir($dh)) !== false) {

if ($file != '.' && $file != '..'){

if (!is_dir($basedir."/".$file)) {

echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";

}else{

$dirname = $basedir."/".$file;

checkdir($dirname);

}

}

}

closedir($dh);

}

}

function checkBOM ($filename) {

global $auto;

$contents = file_get_contents($filename);

$charset[1] = substr($contents, 0, 1);

$charset[2] = substr($contents, 1, 1);

$charset[3] = substr($contents, 2, 1);

if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {

if ($auto == 1) {

$rest = substr($contents, 3);

rewrite ($filename, $rest);

return ("<font color=red>BOM found, automatically removed.</font>");

} else {

return ("<font color=red>BOM found.</font>");

}

}

else return ("BOM Not Found.");

}

function rewrite ($filename, $data) {

$filenum = fopen($filename, "w");

flock($filenum, LOCK_EX);

fwrite($filenum, $data);

fclose($filenum);

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php file function rest gmail xml