John Geliberte
John Geliberte

Reputation: 1025

Remove Next line at the beginning of a string in php

Im planning to remove all the Next line at the beginning of the string, i tried using. str_replace("\n",null,$resultContent) it gives me the result that all Next line are removed.

Example. i need to remove the next line at the beginning of this string

"

String here

String following."

I need to delete the next line at the beginning

Upvotes: 1

Views: 58

Answers (5)

Shashank Shah
Shashank Shah

Reputation: 2167

sounds like ltrim() is what you're looking for:

ltrim — Strip whitespace (or other characters) from the beginning of a string!

echo $result_string = ltrim($string); 

Upvotes: 0

Mikey
Mikey

Reputation: 2704

Not sure whether you just want to strip blank lines, or remove everything after the \n from the first instance of a word... went with the former so hopefully this is what you're after:

$string = "String first line

string second line";


$replaced = preg_replace('/[\r\n]+/', PHP_EOL, $string);
echo $replaced;

Returns:

String first line 
string second line

Upvotes: 0

Atif Tariq
Atif Tariq

Reputation: 2772

You can also use it like this:

if(startsWith($resultContent, '\n')) {  // true:  if string starts with '\n'
    str_replace("\n",null,$resultContent);
}

Upvotes: 0

Please refer this page .

http://www.w3schools.com/php/func_string_trim.asp

use ltrim($resultContent,"\n") to remove all new line chars from starting of string.

Upvotes: 1

Halayem Anis
Halayem Anis

Reputation: 7785

Just explode and take the first result
Do not forget to do some test : if !is_array() .....

$x = explode("\n", $resultContent);
$resultContent = $x[0];

Upvotes: 0

Related Questions