Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$str = "foo:arg1| arg2, bar:arg, baz";
var_dump(strToArray($str));
?>
array(3) {
[0]=>
array(2) {
[0]=>
string(3) "foo"
[1]=>
array(2) {
[0]=>
string(4) "arg1"
[1]=>
string(4) "arg2"
}
}
[1]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
array(1) {
[0]=>
string(3) "arg"
}
}
[2]=>
array(1) {
[0]=>
string(3) "baz"
}
}
<?php
function splitAndStrip($str, $del = ',')
{
$str = explode($del, $str);
for ($i=0; $i < count($str); $i++)
{
$str[$i] = trim($str[$i]);
}
return $str;
}
function strToArray($str)
{
$arr = array();
$str = splitAndStrip($str);
for ($i=0; $i < count($str); $i++)
{
$arr[$i] = splitAndStrip($str[$i], ':');
if(count($arr[$i]) > 1)
{
$arr[$i][1] = splitAndStrip($arr[$i][1], '|');
}
}
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment