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 items in array to a comma separated string in PHP? - Stack Overflow
php - comma-separated string to array - Stack Overflow
php - Convert comma separated string into array - Stack Overflow
php - Array to comma separated string? - Stack Overflow
Use implode:
$tags = implode(', ', array('tag1','tag2','tag3','tag4'));
Yes you can do this by using implode
$string = implode(', ', $tags);
And just so you know, there is an alias of implode, called join
$string = join(', ', $tags);
I tend to use join more than implode as it has a better name (a more self-explanatory name :D )
$array = explode(',', $variable);
In case you string gets a little bit more complex (i.e. elements can be in quotes and both the delimiter and the quoting character can appear within an element) you might also be interested in fgetcsv() and str_getcsv()
$variable = '"left,right","middle", "up,down"';
$row = str_getcsv($variable);
var_dump($row);
prints
array(3) {
[0]=>
string(10) "left,right"
[1]=>
string(6) "middle"
[2]=>
string(7) "up,down"
}
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