Welcome to WebmasterWorld Guest from 54.210.61.41

Forum Moderators: coopster & jatar k

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

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

     
10:20 am on Jan 2, 2019 (gmt 0)

New User

joined:Jan 2, 2019
posts:2
votes: 1


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
10:41 am on Jan 2, 2019 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 322
votes: 24


If I have an array


If you already have "an array" then they are already "split up" and easily retrievable. So, I guess you don't actually have an array?

I can get phrase 1 okay by searching for the first comma and cutting.


If you have a string where the values are separated by commas (CSV) then you can use the explode() function to convert this string into an array (specifying the comma as delimiter):


$myString = 'phrase 1,phrase 2,phrase 3,phrase 4';
$myValues = explode(',',$myString);


Then reference the elements of the array starting at 0. For example:


echo $myValues[0];
echo $myValues[1];
echo $myValues[2];


However, it looks like the delimiter in your "string" might be comma+space? In which case, specify this as the delimiter to the explode() function. Or use the trim() function when you retrieve these values.


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


Not sure what this is refering to? (Some kind of template system?)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members