", '', $str)"."/> ", '', $str)".">
Home > Article > Backend Development > How to remove html line breaks in php
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)".
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); ?>
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>", '', $str); ?>
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"), '', $str);
2)使用正则表达式替换
$str = preg_replace('//s*/', '', $str);
3) Use PHP system constants [Recommended]
$str = str_replace(PHP_EOL, '', $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!