SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How can I extract the first line of a variable?

    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?!

  2. #2
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you should extract the string before "\n".

  3. #3
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mika
    Well, you should extract the string before "\n".
    huh
    Why It Doesn't Work?!

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $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.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your variable doesn't contain any linebreaks as it is. You need to have a ' \n ' in it. Try
    PHP Code:
    <?php
    $myvar 
    "first line\n
    second line\n
    third line\n"
    ;

    $lines explode("\n"$myvar);
    echo 
    $lines[0];
    ?>
    HTH

    -Helge

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, it worked.
    Why It Doesn't Work?!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •