idire Posted October 7, 2008 Share Posted October 7, 2008 Right i had a string, which I split into multiple parts using: $ex = explode(' ', $data); which gives: $ex[0] $ex[1] $ex[2] ... $ex[n] If i wanted to make a new string out of the parts from $ex[2] to $ex[n] how would i do that? e.g. select all the data from a piece onwards. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/ Share on other sites More sharing options...
BillyBoB Posted October 7, 2008 Share Posted October 7, 2008 I would try something like: <?php $ex2[0] = $ex[0]; //Saving the info inside ex[0] optional $ex2[1] = $ex[1]; //Saving the info inside ex[1] optional $ex[0] = ''; $ex[1] = ''; $newstring = implode(' ', $ex); ?> Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659402 Share on other sites More sharing options...
wildteen88 Posted October 7, 2008 Share Posted October 7, 2008 Have a look into array_slice Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659404 Share on other sites More sharing options...
BillyBoB Posted October 7, 2008 Share Posted October 7, 2008 Ah I guess we learn new things everyday... I didn't know about that function. Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659407 Share on other sites More sharing options...
Barand Posted October 7, 2008 Share Posted October 7, 2008 What version PHP? When I try $ex = explode('', $data); I get Warning: explode() [function.explode]: Empty delimiter. in C:\Inetpub\wwwroot\test\noname2.php on line 3 Can't you just $str = 'abcdefg'; echo substr ($str, 2); // cdefg If you must use array $str = 'abcdefg'; $ex = str_split($str); // put chars in array echo join ('', array_splice($ex, 2)); // cdefg Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659410 Share on other sites More sharing options...
BillyBoB Posted October 7, 2008 Share Posted October 7, 2008 Barand, the OP has a space in explodes first parameter. Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659414 Share on other sites More sharing options...
Barand Posted October 7, 2008 Share Posted October 7, 2008 I should have gone to Specsavers Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659426 Share on other sites More sharing options...
idire Posted October 7, 2008 Author Share Posted October 7, 2008 Just worked it out a different way, this is how i did it: <?php for($i = 4; $i < count($ex); $i++) { $cmd4 .= $ex[$i] . " "; } ?> Is there anything wrong with this? very inefficient compared to your codes? Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659427 Share on other sites More sharing options...
Barand Posted October 7, 2008 Share Posted October 7, 2008 it has to count the size each time through the loop and you get an unwanted space at the end. OK, now I know it's a space $str = 'a b c d e f g'; $ex = explode(' ', $str); echo join (' ', array_splice($ex, 2)); // c d e f g Quote Link to comment https://forums.phpfreaks.com/topic/127456-selecting-multiple-parts-of-a-split-array/#findComment-659431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.