Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Removing the last and first line of a variable..

    Urg yep me again - ran into yet another problem...

    I have a variable (some of you may know what project im referring to here) that has many lines of writing in.. i need to remove the first line and the last line...

    Now.. im pretty sure im going to have to use an array for this? which is just my fav thing :/.. I studied Perl for 3 years and still didnt like using arrays :| so when it comes to php im even further away
    but anyway I still had a go tho without using first and last line of the var as I dont know how to get those things, is there a function for that ?.. but here is my attempt at the array stuff.. "pretending" first line is fLine and last line is lLine..

    PHP Code:
    <?
    $myary 
    $fLine."/".$lLine.;
    $pdt explode("/",$myary);

    echo 
    "after: ";
    foreach(
    $pdt as $value)
    {
    echo 
    $value." ";
    }
    but then reliased this will only give me the 2 lines I dont want ?!
    So as you can see - im totally lost with this.. any help would be great..

    Thanks very much.
    Sir P

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $var='line1/xllllllx/line2';
    $var=substr($var, (strpos($var, '/')+1));
    $var=substr($var, 0, strrpos($var, '/'));

    <edit>needs to be done in 2 steps because the strrpos else takes the initial first character as base for the position of /</edit>
    Last edited by raf; 05-03-2004 at 11:33 PM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hey raf.. that returns
    xllllllx
    and only that.. is that a fault of my own? im calling it with $var.

    cheers
    Sir P

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sir pannels
    hey raf.. that returns
    xllllllx
    and only that.. is that a fault of my own? im calling it with $var.

    cheers
    Sir P
    I thought your lines were seperated by '/', so the code i posted will remove everything until the first / and everything behind the last /

    If your lines are seperated with a diferent character, then you need to replace the / inside the code by that seperating character.

    $var is just a dummy variablename here, but it can be anything of course.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    ah yeh i see- due to my try where i used examples..

    well they are seperate by a line break.. but not.. <br> as its not html.. do i need to find and use the ascii code for that?

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try

    $var=substr($var, (strpos($var, "\n")+1));
    $var=substr($var, 0, strrpos($var, "\n"));
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,043
    Thanks
    10
    Thanked 93 Times in 91 Posts
    well rafs is cleaner & probably quicker .... but since you like arrays so much a bit of array-popping etc

    PHP Code:
    <?
    $str
    ='remove this
    but not this
    and definately not this
    snarf - blurgh
    errr this can go as well'
    ;

    /*make an array*/
    $bits =  explode("\n",$str);

    /*remove the first item*/
    array_shift($bits);

    /*remove the last item*/
    array_pop($bits);

    /*rebuild it*/
    echo implode($bits);
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    thats great... i tested both.. and they do both work great - tho due to my fear of array's im gonig with raf's... but ive saved the array under a comment in the code for reference

    Last question.. would what im using here.. rafs code.. how hard would it be to get it to remove everything, no matter what line number before the first apperance of "whatever" ?


    Thank you .. heros

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The array-approach is probably slower and more resource eating anyway (i think )
    Last question.. would what im using here.. rafs code.. how hard would it be to get it to remove everything, no matter what line number before the first apperance of "whatever" ?
    Hmm. Seems like you're not realy inderstanding the code.
    strpos($var, "\n") returns the position of the first occurence of \n inside the string?
    substr returns the part of the string, from the value from the second argument on, until the value of the third argument.
    substr($var, 0, 10) returns the first 10 characters
    substr($var, 10) return the string from the 11° position on, till the end of the string.
    So
    $var=substr($var, (strpos($var, "\n")+1));
    returns the substring from the first occurence of "\n" + 1

    If you replace the "\n" by 'test' then you'll the string from right after 'test' till the end of the string
    Last edited by raf; 05-04-2004 at 10:45 AM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    ohhhhh.... *clicks*

    I get it...it collects the string from whatever character(s) you have in here
    $var=substr($var, (strpos($var, "\n")+1));

    Makes perfect sense now... got it down...
    Thanks very much raf - been a great help, again


    Sir P

  • #11
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're welcome. Happy coding !
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #12
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,043
    Thanks
    10
    Thanked 93 Times in 91 Posts
    Quote Originally Posted by raf
    The array-approach is probably slower and more resource eating anyway
    that was my assumption as well , & I just did a few tests & whilst for small strings arrays are no slower ...for larger ones (& not very large either) it slows down considerably, over twice as slow as rafs method
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    no bloodloose then?!

  • #14
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sir pannels
    no bloodloose then?!
    Hmm. As long as you don't master array, your just a wannabe and we will keep laughing behind your back

    nice going firepages (i was to lazy for that).
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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