search
Homephp教程php手册用file()读取编码替UTF-8的空文本,数组元素个数为什么会显示1

用file()读取编码为UTF-8的空文本,数组元素个数为什么会显示1? ?php $orders=file("$D【本文来自鸿网互联 (http://www.68idc.cn)】OCUMENT_ROOT/../data/order.txt"); $number_of_orders=count($orders); echo"$number_of_orders"; if($number_of_orders1){

用file()读取编码为UTF-8的空文本,数组元素个数为什么会显示1?
 $orders=file("$D【本文来自鸿网互联 (http://www.68idc.cn)】OCUMENT_ROOT/../data/order.txt");
 
 $number_of_orders=count($orders);
 echo"$number_of_orders";
 if($number_of_orders   echo"

No orders pending.
  Please try again later.

";
 }
 
 for($i=0;$i   echo $orders[$i]."
";
 }
?>
刚接触PHP,按照书上例子用以上代码读取TXT文本中的订单数据时,出现数组元素元素个数的问题。
1.新建order.txt文件(默认编码格式为ANSI),不作任何添加和修改,运行代码,显示$number_of_orders=0
2.打开order.txt文件,并另存为编码格式为utf-8的文件,其中不做任何修改,但是$number_of_orders=1
3.修改新建文件的默认编码格式为utf-8,直接新建order.txt文件,还是$number_of_orders=1
4.在order.txt文件中添加一行数据$number_of_orders=1,添加2行数据$number_of_orders=2....(这又正常啊)


问:为什么空文本在编码为UTF-8的时候$number_of_orders的值会是1?那该怎么判断文本是空还是只有一行数据?
------解决思路----------------------
对呀,Windows 系统中,用记事本另存为编码格式为utf-8的文件,会有一个标识文件字符集的 BOM 头

------解决思路----------------------
引用:
Quote: 引用:

对呀,Windows 系统中,用记事本另存为编码格式为utf-8的文件,会有一个标识文件字符集的 BOM 头
果然,那这里应该应该怎么写,怎么判断文本是空?记事本另存会有?那是不是有别的方法创建会没有 BOM 头?

你用其他的文本编辑器就没这个BOM头了,微软就是喜欢非主流。
------解决思路----------------------
BOM 头是用来标志文本文件的字符集的,这是规范的做法
用户可根据 BOM 头正确的解释文本的内容,所以并非非主流
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.