", '', $str)"."/> ", '', $str)".">

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

How to remove html line breaks in php

青灯夜游
青灯夜游Original
2023-01-18 11:16:091905browse

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)
Parameters Description
string Required . 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);
?>

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)

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);
?>

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);
2)使用正则表达式替换
$str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str);

3) Use PHP system constants [Recommended]

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

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!

Statement:
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