Home >Backend Development >PHP Tutorial >How to remove newline characters from string in PHP? (code example)

How to remove newline characters from string in PHP? (code example)

青灯夜游
青灯夜游Original
2019-02-25 10:13:104300browse

In PHP, you can use the built-in functions: str_replace() function or preg_replace() function to delete newlines in strings. This article will show you how these two functions delete newlines. I hope to be helpful.

How to remove newline characters from string in PHP? (code example)

Method 1: Use str_replace() function

str_replace() function can be used Used to find and replace some characters in a string (or array) with other characters (case sensitive). [Video tutorial recommendation: PHP tutorial]

Note:

●If the searched string is an array, then it will be applied to each string in the array. Elements are searched and replaced, and an array is returned.

●If the array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings.

Basic syntax:

str_replace(find,replace,string,count)

Parameters:

find: Specify the string to search and replace.

replace: Specify the string value to be used to replace the value in find.

string: Specify the string to be searched.

count: A variable that counts the number of substitutions, which can be omitted. If this parameter is used, its value is set to the total number of substitution operations performed on the string findl.

Return value: Return a new string or array based on the find parameter with a replacement value.

Example: After replacing the
tag, display the new string in the variable text.

<?php 
 header("content-type:text/html;charset=utf-8");    
//声明变量并用包含<br>标签的字符串初始化它
$text = "Hello<br>World<br>PHP中文网!"; 
  
//显示字符串
echo $text; 
echo "<br>"; 
  
// 使用str_replace()函数删除<br>标签
$text2 = str_replace("<br>", "", $text); 
  
// 显示新字符串
echo $text2; 
?>

Output:

How to remove newline characters from string in PHP? (code example)

Method 2: Use the preg_replace() function

The preg_replace() function can use a regular expression search and replace to find and replace content.

Basic syntax:

preg_replace(pattern, replacement, subject, limit, count)

Parameters:

pattern: Indicates the pattern to be found, which contains the search String element, which can be a string or an array of strings.

replacement: Specify the string or array to replace.

subject: Specify the target string or array that needs to be searched and replaced.

limit: Specifies the maximum number of possible substitutions for each pattern, which can be omitted. The default is -1 (no limit).

count: Indicates the number of replacements that need to be performed and can be omitted.

Return value: If the subject parameter is an array, this function returns an array, otherwise it returns a string.

Example: Use the preg_replace() function to remove newlines.

<?php 
header("content-type:text/html;charset=utf-8");   
//声明变量并用包含<br>标签的字符串初始化它
$text1 = "PHP<br>Java<br>Python!"; 
  
//显示字符串
echo $text1; 
echo "<br>"; 
  
// 使用str_replace()函数删除<br>标签
$text2 = preg_replace( "/<br>|\n/", "", $text1 ); 
$text3 = preg_replace( "/<br>|\n/", "-", $text1 ); 
  
// 显示新字符串
echo $text2; 
echo "<br>".$text3; 
?>

Output:

How to remove newline characters from string in PHP? (code example)

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to remove newline characters from string in PHP? (code example). 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