【整理】解决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);
}
?>
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输出多余的空格或换行
- 【整理】解决php输出文件下载时文件名含中文时出现乱码
- PHP中,<textarea>里的内容保存MYSQL后,页面输出不换行的解决方法
- php fgets函数读取多余的空格解决
- PHP输出到文件时 不会换行问题解决办法
- php将textarea数据提交到mysql出现很多空格的解决方法
- 关于一个字符出现1次或者n次输出问题的解决方法
- 解决html换行出现空格问题
- 解决php输出文件下载时文件名含中文时出现乱码
- js正则表达式,去除首尾多余的空格或者换行
- jsp页面输出HTML产生多余空格(out.print())的解决办法
- log4j设置控制台输出换行后有空格的解决办法
- header()还有setCookie()前面有空格或者输出时,php会出现警告
- php多余的输出(返回值有回车键和空格)
- php使用GD图像库绘制输出图像出现乱码问题和图片上输出中文出现乱码问题解决方法。
- [已解决]scim选字时后出现多余的空格或数字
- <textarea>中嵌套其他标签输出出现不必要的空格和换行问题
- php 出现空格,换行原因
- php将textarea数据提交到mysql出现很多空格的解决方法
- 在X64 RedHat5 或者 RedHat4 下编译安装PHP出现的各种问题及解决