Home > Backend Development > PHP Problem > How to remove html line breaks in php

How to remove html line breaks in php

青灯夜游
Release: 2023-03-17 20:00:02
Original
2028 people have browsed it

Two removal methods: 1. Use the strip_tags() function to delete html line breaks, the syntax is "strip_tags($str,$allow)"; when the "allow" parameter is omitted, all tags will be deleted. If not, all tags will be deleted. The label set by the "allow" parameter will be retained. 2. Use the str_replace() function to replace the html newline character with a null character. The syntax is "str_replace("
", '', $str)".

How to remove html line breaks in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php removes html line breaks Method

Method 1: Use the strip_tags() function

strip_tags() function to strip HTML, XML and PHP tags from the string .

Comments: This function always strips HTML comments. This cannot be changed via the allow parameter.

Note: This function is binary safe.

strip_tags(string,allow)
Copy after login
ParametersDescription
stringRequired . Specifies the string to check.
allow Optional. Specifies allowed tags. These tags will not be deleted.

Return value: Returns the stripped string.

Example:

<?php
$str="Hello<br> world!";
echo $str."<hr>";
echo strip_tags($str);
?>
Copy after login

How to remove html line breaks in php

Method 2: Use str_replace() function

str_replace() function to replace characters Some characters in the string (case sensitive), returns a string with replacement value.

str_replace(find,replace,string,count)
Copy after login

Just use the str_replace() function to replace the html newline character "
" with a null character.

<?php
$str="Hello<br> world!";
echo $str."<hr>";
echo str_replace("<br>", &#39;&#39;, $str);
?>
Copy after login

How to remove html line breaks in php

Extended knowledge: How to remove line breaks in PHP - using the str_replace() function

str_replace() function is used Replace some characters in a string.

Implementation code:

1) Use the escape character function

$str = str_replace(array("/r/n", "/r", "/n"), &#39;&#39;, $str);
Copy after login
2)使用正则表达式替换
Copy after login
$str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str);
Copy after login

3) Use PHP system constants [Recommended]

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

Recommended learning: "PHP video tutorial

The above is the detailed content of How to remove html line breaks in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template