Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove newline character from a string using PHP regex

Tags:

php

How can I remove a new line character from a string using PHP?

like image 657
coderex Avatar asked Jan 02 '10 10:01

coderex


People also ask

How remove all new lines from a string in PHP?

The line break can be removed from string by using str_replace() function.

Does PHP trim Remove newline?

Yes it does, see the manual: This function returns a string with whitespace stripped from the beginning and end of str .

How can I remove one character from a string in PHP?

Explanation: In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known. $str = "geeks" ; // Or we can write ltrim($str, $str[0]);


2 Answers

$string = str_replace(PHP_EOL, '', $string); 

or

$string = str_replace(array("\n","\r"), '', $string); 
like image 112
AntonioCS Avatar answered Sep 21 '22 07:09

AntonioCS


$string = str_replace("\n", "", $string); $string = str_replace("\r", "", $string); 
like image 40
Harmen Avatar answered Sep 19 '22 07:09

Harmen