`
天梯梦
  • 浏览: 13705226 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

PHP_EOL 用法 : Most Worthless Constant?

 
阅读更多

PHP_EOL may very well be the most worthless general-purpose constant in modern PHP. It's supposed to be helpful for cross-platform developing, for example you could write a PHP-powered shell script that says:

<?php
echo "Operation Successful!" . PHP_EOL;

 

and then expect the proper newline to terminate the output string based on the platform PHP is running on.

That's all well and good, but the following is functionally equivalent:

<?php
echo "Operation Successful!\n";

 

Try it out and you'll see. In console output on Windows, Linux, and Mac they all are displayed with the expect newline terminating the output string.

 

I don't see it being useful for writing data or log output to a file either. If you're writing and reading on the same platform then newline discrepancies won't be an issue, and if you're writing on one platform and reading on another then you'll want to standardize on a newline anyway.

 

Has PHP_EOL's time come and gone? Do you use it in your code, and if so why?

 

from: http://zaemis.blogspot.com/2012/09/phpeol-most-worthless-php-constant.html

 

一个小小的换行,其实在不同的平台有着不同的实现,为什么要这样,可以是世界是多样的。

回车换行其实是两个符号

 

\r 回车  //回到行首   return

\n换行   //重新换一行 newline

 

一般情况下\n和\r\n是没有区别的,因为一般情况下,下一行是空白的,没有内容的,但是如果下一行有内容,那么两者的不同就会体现出来,\r\n光标会显示在新行的行首,而\n的光标则会显示在新行的字符结尾处

 

本来在unix世界换行就用\n来代替,但是windows为了体现他的不同,就用\r\n,更有意思的是在mac中用\r

因此unix系列用 \n

windows系列用 \r\n

mac用 \r

 

这样就用你写的程序在不同的平台上运行有着不少的麻烦

比如有的程序要把文件中的所有行都合成一行,这有不同的实现方式,

 

第一种方式

str_replace(array("\r","\n","\r\n"),"",$string);

 

 

第二种方式就用正则表达示

$str = preg_replace('/\s*/', '', $str);

 

第三种方式

这里不得不重新看一下php 那些已经定义好的变量

PHP_EOL 就是其中的一个,代表php 的换行符,这个变量会根据平台而变,在windows下会是\r\n,在linux下是\n,在mac下是\r

$str = str_replace(PHP_EOL, '', $str);

 

 

来源: http://blog.sina.com.cn/s/blog_6dbaeb9b0100z4tt.html

 

 

分享到:
评论

相关推荐

    PHP中去除换行解决办法小结(PHP_EOL)

    使用方法如下: ```php $str=str_replace(PHP_EOL, "", $str); ``` 这段代码会根据运行代码的操作系统自动去除相应的换行符。 在实际应用中,开发者可以根据实际情况选择最合适的方法。通常情况下,推荐使用PHP...

    Form_builder内置函数说明

    15. `clear_eol`: 清除当前行从光标到行尾的文本项值。 16. `clear_form`: 清除表单中所有记录,重置表单状态。 17. `commit_mode`: 提供不同提交模式,如提示用户提交、自动提交或不提交。 18. `clear_item`: ...

    FORM-Builder常用函数.doc

    30. `create_queried_record`: 在块的主记录缓存中创建新记录,通常在`on_fetch`触发器中使用。 31. `create_record`: 在当前块中创建新记录,为用户提供新的空白输入区域。 32. `create_time`: 创建一个定时器,...

    php中的PHP_EOL换行符详细解析

    在unix系列用 \n在windows系列用 \r\n在mac用 \rPHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 比如: 复制代码 代码如下: echo PHP_EOL; //windows平台相当于 echo “\r\n”; //unix\linux平台相当...

    PHP预定义常量详解.md

    PHP作为一种广泛使用的开源脚本语言,不仅支持快速构建动态网页,还提供了丰富的内置功能。其中,预定义常量是在PHP脚本执行过程中始终可用的一类特殊变量,它们无需事先声明即可直接使用。预定义常量覆盖了诸如版本...

    Oracle Form觸發器、系統變量精解2

    根据提供的文件信息,本文将对Oracle Form触发器与系统变量进行深入解析,特别是关于触发器在Oracle Forms中...开发者应该熟练掌握这些工具的使用方法,并且根据具体的应用场景选择合适的触发器和系统变量来进行开发。

    AU9540,AU9560,AU9580_EOL_Letter_202109(1).pdf

    AU9540,AU9560,AU9580_EOL_Letter_202109(1).pdf 本文档是Alcorlink Corp.发布的一份产品停产通知,主要针对AU9540、AU9560和AU9580智能卡reader产品。该通知日期为2021年9月1日,旨在告知客户这三款产品将逐步停产...

    php去除换行符的方法小结(PHP_EOL变量的使用)

    下面是PHP去除换行符的一些常见方法。 第一种写法: 复制代码 代码如下:$content=str_replace(“\n”,””,$content);echo $content; 第二种写法: 复制代码 代码如下:str_replace(“\r\n”,””,$str);

    eol_terms:EOL在站点上维护的术语,其URI及其属性的非常基本的存储库

    eol_terms EOL在站点上维护的术语,其URI及其属性的非常基本的存储库。 用法 入门 将以下内容添加到您的Gemfile中: gem 'eol_terms', git: 'https://github.com/EOL/eol_terms.git' 工作流程 有人编辑terms.yml...

    chef_worldping_api

    注意:worldPing已于2021年4月1日到达使用寿命终止(EOL) 现在,您可以通过Grafana Cloud中新的综合监控功能获得有关worldPing的所有信息,此外,您还将获得降低的复杂性以及Grafana Cloud的所有优势。 通过注册...

    Requests-HTTP库.zip

    &lt;?php if (!isset($argv[1]) || $argv[1] === '-h' || $argv[1] === '--help') { echo 'usage: php ' . $argv[0] ....但是如果PHP的cURL可用,Requests会优先使用它,否则会使用socket。

    eol_python:Python中的《生命百科全书》 API的包装器

    安装克隆此仓库,并将eol_api_wrapper.py导入为模块: &gt;&gt; &gt; import eol_api_wrapper as eol用法要开始访问API,请首先实例化API类的实例。 如果您打算对API进行大量调用,请按所述生成API密钥并添加为参数: &gt;&gt; &gt; ...

    java6.0源码-capstone_eol:Capstone2011fork或eol代码

    编写的,任何人都可以重新使用、重新利用或改进。 这既是一个雄心勃勃的项目,也是一个雄心勃勃的代码库,我们很高兴与开源社区分享它。 该代码自 2007 年 9 月左右开始开发,但在 2008 年 6 月至 2008 年 12 月期间...

    利用phpexcel把excel导入数据库和数据库导出excel实现

    复制代码 代码如下:&lt;?php/**author zhy*date 2012 06 12*for excel*/date_default_timezone_set(“PRC...define(‘EOL’,(PHP_SAPI == ‘cli’) ? PHP_EOL : ‘ ’); require_once (‘../Classes/PHPExcel.php’)

    StockMarketApp-ionic1:在直观设计的应用程序中关注您最喜欢的股票

    :red_exclamation_mark: 注意: This project has reached its EOL, and will no longer be maintained. :red_exclamation_mark: 以下项目涉及使用开发交互式股票市场应用程序,然后使用和将其部署在Android平台上...

    php代码-PHP常量

    - `__METHOD__`: 当前方法的名称(包括类名)。 7. **使用const和define的比较** `const`关键字在类内部定义常量,而`define()`函数可以在任何地方定义。`const`要求常量值必须是静态的(在编译时已知),而`...

Global site tag (gtag.js) - Google Analytics