1

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

John Geliberte
  • 935
  • 1
  • 10
  • 19

5 Answers5

1

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.

0

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

$x = explode("\n", $resultContent);
$resultContent = $x[0];
Halayem Anis
  • 7,485
  • 2
  • 22
  • 42
0

You can also use it like this:

if(startsWith($resultContent, '\n')) {  // true:  if string starts with '\n'
    str_replace("\n",null,$resultContent);
}
Atif Tariq
  • 2,512
  • 26
  • 33
0

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
Mikey
  • 2,426
  • 1
  • 11
  • 20
0

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); 
Shashank Shah
  • 1,921
  • 4
  • 18
  • 42