Forum Moderators: coopster

Message Too Old, No Replies

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

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

         

huongtranbong

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

5+ Year Member



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

whitespace

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

10+ Year Member Top Contributors Of The Month



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?)