php换行替换的方法:1、使用str_replace来替换换行,语法如“str_replace(array("\\r\\n", "\\r", "\\n"), "", $str);”;2、使用正则表达式替换;3、使用php定义好的变量替换。

推荐:《PHP视频教程》
PHP中替换换行符的几种方法:
第一种:
代码如下:
<?php
?$str="this is a test \\n";
$patten = array("\\r\\n", "\\n", "\\r");
?//先替换掉\\r\\n,然后是否存在\\n,最后替换\\r
$str=str_replace($order, "", $str);
?>
//php 有三种方法来解决
//1、使用str_replace 来替换换行
$str = str_replace(array("\\r\\n", "\\r", "\\n"), "", $str);
//2、使用正则替换
$str = preg_replace('//s*/', '', $str);
//3、使用php定义好的变量 (建议使用)
$str = str_replace(PHP_EOL, '', $str);代码如下:
/*
* 获得用户操作系统的换行符,\\n
* @access public
* @return string
*/
function get_crlf()
{
if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win'))
{
$the_crlf = '\\r\\n';
}
elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac'))
{
$the_crlf = '\\r'; // for old MAC OS
}
else
{
$the_crlf = '\\n';//权重大一点
}
return $the_crlf;
}注意:在前台页面显示的时候,用nl2br使换行变成
第二种实例说明:
发现一个有趣的事情:
$text="aaaa ccc"; $text=str_replace('\\n‘,"",$text); $text=str_replace('\\r‘,"",$text); $text=str_replace('\\r\\n‘,"",$text);
正常来说,上面的代码应该可以替换换行符了吧
但是事实上却是不可以!
很郁闷,试了很多次,就是不起作用。
最后改成这样
代码如下:
$text=str_replace("\\n","",$text);
$text=str_replace("\\r","",$text);
$text=str_replace("\\r\\n","",$text);居然一切OK了~~,原来是双引号,单引号的问题!!
双引号 比单引号效率差点,因为双引号在被php解析的过程中 ,还会判断里面会不会有变量,单引号就不会有这个判断,故而一般来讲,没涉及到变量的情况下,我都会用单引号,没想到这次替换换行符,用单引号居然不行·····
最后写成一句话
代码如下:
$order = array("\\r\\n", "\\n", "\\r");
$replace = '';
$text=str_replace($order, $replace, $text);这样即可替换换行符!
The above is the detailed content of How to replace newline in php. For more information, please follow other related articles on the PHP Chinese website!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool






