Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-07-2011, 04:35 PM   PM User | #1
acm2011
New to the CF scene

 
Join Date: Apr 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
acm2011 is an unknown quantity at this point
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
acm2011 is offline   Reply With Quote
Old 04-07-2011, 04:44 PM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
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); 
__________________
Ushousebuilders.com

Last edited by poyzn; 04-07-2011 at 04:49 PM..
poyzn is offline   Reply With Quote
Old 04-07-2011, 04:56 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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);

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
acm2011 (04-07-2011)
Old 04-07-2011, 06:41 PM   PM User | #4
acm2011
New to the CF scene

 
Join Date: Apr 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
acm2011 is an unknown quantity at this point
Thanks for the response, array_slice() did exactly what I needed.
acm2011 is offline   Reply With Quote
Old 04-07-2011, 07:58 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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.
__________________
ZCE
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 PM.


Advertisement
Log in to turn off these ads.