For example I have code like this: $cities = "resultA, resultB, resultC, resultD, resultE";
Now, I want to retrieve the results to be like this : $finalcities = "[resultA] [resultB] [resultC] [resultD] [resultE]";
Is anybody can help me how to make it as I want? thanks.
to convert the string
$cities = "resultA, resultB, resultC, resultD, resultE";
to
$finalcities = "[resultA] [resultB] [resultC] [resultD] [resultE]";
OPTION A
$finalcities = '['.str_replace(', ','] [',$cities).']';
simple str_repalce of the, comma-space, separator plus the outer []
OPTION B
$finalcities = '['.implode('] [',explode(', ',$cities)).']';
little fancier, first split the string with explode then glue it back together with implode
Just one line of code.
$cities = '['.str_replace(', ','] [', $cities).']';
We are replacing ', ', with '] [', so it will result in this
resultA][resultB][resultC][resultD][resultE
Now just add '[' and ']' to start and end.
Just add [ and ] bracket from start and end of the string and , will be replaced by ][ string.