1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Remove line from the beginning of a string?

Discussion in 'PHP & Perl' started by HelloInsomnia, Feb 25, 2012.

  1. HelloInsomnia

    HelloInsomnia Jr. Executive VIP Jr. VIP

    Joined:
    Mar 1, 2009
    Messages:
    1,822
    Likes Received:
    2,925
    I am a total PHP nub - it's been years since I attempted to make anything. Finally I had a good idea and decided to give it a shot. Long story short I'm stuck on a problem.

    Here is the issue:

    Code:
    foreach(explode("\n", $x) as $y) {
    
    I want to display $y in a line with a bunch of stuff in front of it and after it on the same line.

    But it keeps adding a line break into the string because it has to go to the new line to get the input (this is taking it from a textarea with many lines).

    I tried trim and ltrim and those didn't seem to work.

    Any help would be awesome.
     
  2. appman360

    appman360 Newbie

    Joined:
    Sep 21, 2011
    Messages:
    12
    Likes Received:
    5
    sometimes the textarea contains \r\n - maybe that's the problem. If so, you can use replace them before exploding.

    Haven't tested but maybe something like this :

    PHP:
     $x preg_replace('/\r\n|\r/'"\n"$x);
    foreach(
    explode("\n"$x) as $y) {
     
    • Thanks Thanks x 1
  3. paxpelus

    paxpelus Junior Member

    Joined:
    Feb 7, 2011
    Messages:
    159
    Likes Received:
    50
    • Thanks Thanks x 1
  4. HelloInsomnia

    HelloInsomnia Jr. Executive VIP Jr. VIP

    Joined:
    Mar 1, 2009
    Messages:
    1,822
    Likes Received:
    2,925
    Thanks, Appman360 your solution worked