How to solve invalid and extra newlines in PHP strings

autoload
Release: 2023-03-09 09:36:02
Original
2000 people have browsed it

How to solve invalid and extra newlines in PHP strings

Invalid newline character:

Example:

Copy after login

The above problem Mainly because in PHPsingle quotesanddouble quotescan be expressed, but double quotes also have a special function: the content in the double quote string can be interpreted and replaced. This is What single quotes don’t have.

After modification:

Copy after login

Excess newlines:

//php 不同系统的换行 //不同系统之间换行的实现是不一样的 //linux 与unix中用 /n //MAC 用 /r //window 为了体现与linux不同 则是 /r/n //所以在不同平台上 实现方法就不一样 //php 有三种方法来解决
Copy after login

1. Use str_replace to replace newlines

$str = str_replace(array("/r/n", "/r", "/n"), "", $str);
Copy after login

2. Use regular replacement

$str = preg_replace('//s*/', '', $str);
Copy after login

3. Use variables defined by php

$str = str_replace(PHP_EOL, '', $str);
Copy after login

Recommended: "2021 PHP interview questions summary (collection)" "php video tutorial"

The above is the detailed content of How to solve invalid and extra newlines in PHP strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!