Skip to content

Instantly share code, notes, and snippets.

@clarkeash
Last active December 22, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkeash/6516386 to your computer and use it in GitHub Desktop.
Save clarkeash/6516386 to your computer and use it in GitHub Desktop.
<?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