php remove newline – How to remove newlines (characters) from a string in php?

php remove newline – Using The str_replace() function and Remove new lines from string and replace with one empty space. The line break can be removed from string by using str_replace() function.

$user_line = trim(preg_replace('/\s+/', ' ', $user_line));

php remove newline

Remove new lines from string and replace with one empty space

Remove newlines using regex.

$user_line = trim(preg_replace('/\s\s+/', ' ', $user_line));

$text = preg_replace("/\r|\n/", "", $text);

PHP: Remove newlines from text

Example

<?php

$text = "Welcome
    
Free Download Source code with demo and example 
";
echo nl2br($text);

$text = str_replace(array("\n", "\r"), '', $text);

echo nl2br($text);

Remove line breaks with PHP

To remove line breaks using PHP see example:

//Removes all 3 types of line breaks
$user_line = str_replace("\r", "", $user_line);
$user_line = str_replace("\n", "", $user_line);

Python Remove Newline

Read Also:  How To Import CSV File into MySQL using PHP

Complete code to remove newlines (characters) from a string in PHP:

<?php
$user_line = "Pakainfo, w3diy, infinityknow,\nWe are very golden news.\nUp above the world therefor high,\nLike a diamond in the website.";

$user_line = str_replace("\n", "", $user_line);
$user_line = str_replace("\r", "", $user_line);

echo $user_line;
?>

I hope you get an idea about php remove newline.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.