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
02 |
header( ‘content-Type: text/html; charset=utf-8‘ ); |
03 |
$auto =1; /*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/ |
05 |
$loop =true; //www.phpernote.com |
06 |
echo ‘当前查找的目录为:‘ . $basedir . ‘当前的设置是:‘ ; |
07 |
echo ‘(1)‘ , $loop ? ‘检查当前目录以及当前目录的子目录‘ : ‘只针对当前目录进行检测‘ ; |
08 |
echo ‘(2)‘ , $auto ? ‘检测文件BOM同时去除检测到BOM文件的BOM<br />‘ : ‘只检测文件BOM不执行去除BOM操作<br />‘ ; |
10 |
checkdir( $basedir , $loop ); |
11 |
function checkdir( $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> ' |
20 |
$dirname = $basedir . ‘/‘ . $file ; |
28 |
function checkBOM( $filename ){ |
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){ |
36 |
$rest = substr ( $contents ,3); |
37 |
rewrite( $filename , $rest ); |
38 |
return ( ‘ <font color=red>找到BOM并已自动去除</font>‘ ); |
40 |
return ( ‘ <font color=red>找到BOM</font>‘ ); |
46 |
function rewrite( $filename , $data ){ |
47 |
$filenum = fopen ( $filename , ‘w‘ ); |
48 |
flock ( $filenum ,LOCK_EX); |
49 |
fwrite( $filenum , $data ); |
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