Resolve extra spaces or line breaks in PHP output

Source: Internet
Author: User

Using the CI framework to write the app backend interface, the return of the JSON front there is an extra 2 elder brother Line, the first to troubleshoot the BOM, the result is still the problem

And then to troubleshoot <?php?> tag, there is no extra return, line, the results found that there is indeed an extra line, removed, the problem disappears.

Summary: It is recommended to remove the PHP end tag, which avoids a lot of inexplicable problems

Write code of children's shoes should know, often in front of the page can not be pinned, even if you add a lot of style, not, then you can consider whether it is the following situation:

1. Confirm that the code does not have echo or exit space and line change;

2, ensure that the PHP file <?php?> label outside the no extra carriage return, line change;

3, more than two after the troubleshooting, if your code file is UTF8 encoded file, it may be considered due to the BOM file header, you can use any non-Microsoft-made text editor (such as notepad++, UltraEdit, etc.) to open the file, save as a BOM-free file, If there are too many files, you can save the following code and put it in the root directory to execute once!

<?php//remove the Utf-8 bomsif (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))!== F Alse) {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 BOM Removal tool, PHP batch removal of BOM code

PHP batch removal of file BOM code function, Support folder batch removal. The specific PHP file code is as follows:

View Code Printing
01 <?php
02 header(‘content-Type: text/html; charset=utf-8‘);
03 $auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/
04 $basedir=‘.‘;
05 $loop=true;//www.phpernote.com
06 echo‘当前查找的目录为:‘.$basedir.‘当前的设置是:‘;
07 echo‘(1)‘,$loop?‘检查当前目录以及当前目录的子目录‘:‘只针对当前目录进行检测‘;
08 echo‘(2)‘,$auto?‘检测文件BOM同时去除检测到BOM文件的BOM<br />‘:‘只检测文件BOM不执行去除BOM操作<br />‘;
09
10 checkdir($basedir,$loop);
11 functioncheckdir($basedir=‘‘,$loop=true){
12     $basedir=empty($basedir)?‘.‘:$basedir;
13     if($dh=opendir($basedir)){
14         while(($file=readdir($dh))!==false){
15             if($file!=‘.‘&&$file!=‘..‘){
"    ;                if (! is_dir $basedir '/' $file
   ;                   echo   $basedir '/' $file  .checkbom ( $basedir '/' $file ' <br> '
18                 }else{
19                     if(!$loopcontinue;
20                     $dirname=$basedir.‘/‘.$file;
21                     checkdir($dirname);
22                 }
23             }
24         }
25         closedir($dh);
26     }
27 }
28 functioncheckBOM($filename){
29     global$auto;
30     $contents=file_get_contents($filename);
31     $charset[1]=substr($contents,0,1);
32     $charset[2]=substr($contents,1,1);
33     $charset[3]=substr($contents,2,1);
34     if(ord($charset[1])==239&&ord($charset[2])==187&&ord($charset[3])==191){
35         if($auto==1){
36             $rest=substr($contents,3);
37             rewrite($filename,$rest);
38             return(‘ <font color=red>找到BOM并已自动去除</font>‘);
39         }else{
40             return(‘ <font color=red>找到BOM</font>‘);
41         }
42     }else{
43         return(‘ 没有找到BOM‘);
44     }
45 }
46 functionrewrite($filename,$data){
47     $filenum=fopen($filename,‘w‘);
48     flock($filenum,LOCK_EX);
49     fwrite($filenum,$data);
50     fclose($filenum);
51 }

Save the above code as a file with the suffix PHP in the file directory where the BOM needs to be removed, and then run the PHP file, the directory and all subdirectories of the directory will be checked for BOM and the BOM is removed, the running results page is as follows:





Resolve extra spaces or line breaks in PHP output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.