Home > Backend Development > PHP Problem > php php_eol usage

php php_eol usage

青灯夜游
Release: 2023-03-06 09:20:01
Original
3908 people have browsed it

php Usage of php_eol: replace the newline character, which will change according to the platform. It is "/r/n" under windows, "/n" under linux, and "/r" under mac; generally it can be used "str_replace(PHP_EOL,'',string)" to remove newlines, compatible with all platforms.

php php_eol usage

Recommended: "PHP Video Tutorial"

A small line break actually has different effects on different platforms The realization of why this is so can be that the world is diverse. Originally, /n was used to replace line breaks in the Unix world, but in order to reflect the difference, Windows uses /r/n. What is more interesting is that /r is used in Mac. Therefore, the Unix series uses /n, the Windows series uses /r/n, and the Mac uses /r. This will cause a lot of trouble for the program you write to run on different platforms. Here are some common ways to remove newlines in PHP.

The first way of writing:

$content=str_replace("\n","",$content); 
echo $content;
Copy after login

The second way of writing:

str_replace("\r\n","",$str);
Copy after login

The third way of writing:

$content=preg_replace("/\s/","",$content); 
echo $content;
Copy after login

About\n,\ r,\t

  • \n Soft return: In Windows, it means a line break and returns to the beginning of the next line. In Linux and Unix, it only means a line break, but Does not go back to the beginning of the next line.

  • \r Soft space: In Linux and Unix, it means returning to the beginning of the current line. In Mac OS, it means breaking a line and returning to the beginning of the next line, which is equivalent to the effect of \n in Windows.

  • \t Tab (move to the next column)

A few notes:

They are enclosed in double quotes or fixed It is valid in strings represented by delimiters and invalid in strings represented by single quotes.
\r\n is generally used together to represent the Enter key on the keyboard (in Linux and Unix), or only \n (in Windwos). In Mac OS, \r is used to represent Enter.
\tRepresents the "TAB" key on the keyboard.
Line break symbol in the file: windows: \n, linux, unix: \r\n

Supplementary code:

<?php 
//php 不同系统的换行 
//不同系统之间换行的实现是不一样的 
//linux 与unix中用 /n 
//MAC 用 /r 
//window 为了体现与linux不同 则是 /r/n 
//所以在不同平台上 实现方法就不一样 
//php 有三种方法来解决 
//1、使用str_replace 来替换换行 $str = str_replace(array("/r/n", "/r", "/n"), "", $str); 
//2、使用正则替换 $str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str); 
//3、使用php定义好的变量 (建议使用) $str = str_replace(PHP_EOL, &#39;&#39;, $str); 
?>
Copy after login

PHP_EOL is a defined variable, representing php Newline character, this variable will change according to the platform. It will be /r/n under windows, /n under linux, and /r under mac.

To remove line breaks and line breaks, just press the following:

$str = str_replace(PHP_EOL, &#39;&#39;, $str);
Copy after login

Related recommendations: php training

The above is the detailed content of php php_eol usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template