Let's say I have the fallowing variable:
$myvar = 'first line
second line
third line';
I need 'first line' in a sperate variable.
Do you know how can I do that?
Thank you.
| SitePoint Sponsor |
Let's say I have the fallowing variable:
$myvar = 'first line
second line
third line';
I need 'first line' in a sperate variable.
Do you know how can I do that?
Thank you.
Why It Doesn't Work?!





Well, you should extract the string before "\n".
huhOriginally Posted by Mika
![]()
Why It Doesn't Work?!
$line1 = substr($myvar, 0, strpos($myvar, "\n"));
(but make sure you only have \n line breaks)
Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.





Your variable doesn't contain any linebreaks as it is. You need to have a ' \n ' in it. TryHTHPHP Code:<?php
$myvar = "first line\n
second line\n
third line\n";
$lines = explode("\n", $myvar);
echo $lines[0];
?>
-Helge
No you don't, that would put 2 line breaks.
PHP does line breaks in multi-line variables.
Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.





Ooops, sorry. Ignore my stupidity.
That mistake tells me that I need to stop working now. Have a nice evening.
But you can still use the explode() function with same separator then.
-Helge
Thanks guys, it worked.
Why It Doesn't Work?!
Bookmarks