Welcome to WebmasterWorld Guest from 34.235.143.190

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Finding the 2nd, 3rd, 4th etc. phrase in a comma-separated array?

     
2:03 pm on Oct 13, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
posts:2528
votes: 0


If I have an array with:

phrase 1, phrase 2, phrase 3, phrase 4

How do I split it up to have them retrieved individually? I can get phrase 1 okay by searching for the first comma and cutting. But what about the rest?

%%phrase1%% = phrase 1
%%phrase2%% = phrase 2
%%phrase3%% = phrase 3
%%phrase4%% = phrase 4

Help please!

Thanks
Mike
2:14 pm on Oct 13, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3127
votes: 1


If I have an array...


If you have an array, then you simply reference $myarray[0], $myarray[1], etc. to access the 1st, 2nd, etc. element.

But, I assume from the rest of your post that you don't actually have an array... possible a comma separated string?

In which case, make an array from the string by exploding it...

$mystring = 'phrase 1, phrase 2, phrase 3, phrase 4';
$myarray = explode(',',$mystring);
echo $myarray[0]; // phrase 1
echo $myarray[1]; // phrase 2
// etc.


Is the space after the comma delimiter a reality/problem?
1:41 pm on Oct 15, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 15, 2004
posts:942
votes: 0


use trim() for the space after the comma
echo trim($myarray[0]); // phrase 1

assuming that your extra space is not needed
10:11 am on Dec 9, 2014 (gmt 0)

New User

joined:Dec 9, 2014
posts:1
votes: 0


Use this function
explode() function breaks a string into an array.The "separator" parameter cannot be an empty string.
$mystr='ph1,ph2,ph3';
$myarray=explode(',',$mystr);
echo $myarray[0];
echo $myarray[1];
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members