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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Putting part of an array into a string

    So I have split up a field of text into an array of strings like the following:

    $body_words = explode(" ", $msg_body);

    But now I want to take a portion of this array, say $body_words[3] through the end of the array and put them back together into a string, leaving out the beginning of the array like $body_words[0], [1], and [2]. I havent found a good solution yet for this and would really appreciate the help.

    Thanks

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    $msg_body 'some words and phrases';
    $body_words explode(" "$msg_body);
    unset(
    $body_words[3]); // or unset(end($body_words));
    echo implode(' '$body_words); 
    or
    PHP Code:
    $msg_body 'some words and phrases';
    $body_words explode(" "$msg_body);
    $last_word array_pop($body_words);
    echo 
    $last_word '<br />';
    echo 
    implode(' '$body_words); 
    Last edited by poyzn; 04-07-2011 at 04:49 PM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by poyzn View Post
    PHP Code:
    $msg_body 'some words and phrases';
    $body_words explode(" "$msg_body);
    unset(
    $body_words[3]); // or unset(end($body_words));
    echo implode(' '$body_words); 
    or
    PHP Code:
    $msg_body 'some words and phrases';
    $body_words explode(" "$msg_body);
    $last_word array_pop($body_words);
    echo 
    $last_word '<br />';
    echo 
    implode(' '$body_words); 
    Neither of these will do what you want. The first removes the 3rd element and then rejoins, while the second removes the last item and then joins.
    What you want is array_slice.
    PHP Code:
    $body_words explode(" "$msg_body);
    if (
    $aSlice array_slice($body_words3))
    {
        print 
    implode(' '$aSlice);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    acm2011 (04-07-2011)

  • #4
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the response, array_slice() did exactly what I needed.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by poyzn View Post
    PHP Code:
    // or unset(end($body_words)); 
    Just for the sake of future knowledge: nah that won't work.


  •  

    Tags for this Thread

    Posting Permissions

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