Community warning: If that string comes from a csv file, use of
str_getcsv()instead is strictly advised, as suggested in this answer
Try explode:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
Answer from Matthew Groves on Stack OverflowCommunity warning: If that string comes from a csv file, use of
str_getcsv()instead is strictly advised, as suggested in this answer
Try explode:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
$string = '9,admin@google.com,8';
$array = explode(',', $string);
For more complicated situations, you may need to use preg_split.
how to convert comma separated string to array in array - PHP Coding Help - PHP Freaks
php - Convert comma separated string into array - Stack Overflow
php - Array to comma separated string? - Stack Overflow
Generate Array from a comma-separated list - PHP - Stack Overflow
Just like you exploded you can implode again:
$tags = explode(',', $arg);
foreach ($tags as &$tag) {
$tag = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
}
return implode(', ', $tags);
Here's an alternative that uses array_map instead of the foreach loop:
global $u;
function add_html($tag){
return('<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>');
}
function get_tags($taglist){
$tags_arr = array_map("add_html", explode( ',' , $taglist));
return implode(", " , $tags_arr);
}
Check out explode: $thePostIdArray = explode(', ', $var);
use explode function. this will solve your problem. structure of explode is like this
array explode ( string $delimiter , string $string [, int $limit ] )
now $delimiter
is the boundary string, string $string is the input string.
for limit:
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
If the limit parameter is negative, all components except the last -limit are returned.
If the limit parameter is zero, then this is treated as 1.
visit the following link. you can learn best from that link of php.net
You can use explode() to achieve this.
$str = "a,b,c";
$array = array();
foreach( explode( ',', $str ) as $v )
{
$array[ $v ] = $v;
}
echo '<pre>'.print_r( $array, true ).'</pre>';
Using explode() and array_combine():
$str = "a,b,c";
$array = explode(',', $str);
$array = array_combine($array, $array);