Reputation: 3243
I have a string
p, br, a[href,title], ul, li, em, strong
I want to convert this to array. I tried to use explode
but it do not work as expected:
$tags = explode(',', $tags);
Above code output:
array (
0 => 'p',
1 => ' br',
2 => ' a[href',
3 => 'title]',
4 => ' ul',
5 => ' li',
6 => ' em',
7 => ' strong'
)
What should I do so I can get result like this:
array (
0 => 'p',
1 => ' br',
2 => ' a' => array('href', 'title'),
4 => ' ul',
5 => ' li',
6 => ' em',
7 => ' strong'
)
Upvotes: 1
Views: 71
Reputation: 14532
The string actually consists of three RegEx patterns: tags with params (like a[href,title]
), tags without params and with a comma after it (like br
), abd the last tag without params.
$patterns = [
'tagWithoutParams' => '/(\w+)\,/',
'lastTagWithoutParams' => '/\w+$/',
'tagWithParams' => '/\w+\[\w+[,\w]*\]/',
];
$matches = [
'tagWithoutParams' => [],
'lastTagWithoutParams' => [],
'tagWithParams' => [],
];
$string = 'p, br, a[href,title], img[src,style], ul, li, em, strong';
preg_match_all($patterns['tagWithoutParams'], $string, $matches['tagWithoutParams']);
preg_match_all($patterns['lastTagWithoutParams'], $string, $matches['lastTagWithoutParams']);
preg_match_all($patterns['tagWithParams'], $string, $matches['tagWithParams']);
$tags = array_merge(
$matches['tagWithoutParams'][1],
$matches['lastTagWithoutParams'][0],
$matches['tagWithParams'][0]
);
/*
Another variant:
$patterns = ['tagWithoutParams'] => '/\w+\,/';
and then
array_walk($matches['tagWithoutParams'][0], function (&$value) {
$value = str_replace(',', '', $value);
});
and
$tags = array_merge(
$matches['tagWithoutParams'][0],
$matches['lastTagWithoutParams'][0],
$matches['tagWithParams'][0]
);
*/
foreach ($tags as $key => $tag) {
if (strpos($tag, '[')) {
$tagName = substr($tag, 0, strpos($tag, '['));
$paramsString = substr($tag, strpos($tag, '[') + 1, (strpos($tag, ']') - strpos($tag, '[') - 1));
$paramsArray = explode(',', $paramsString);
$tags[$key] = [$tagName => $paramsArray];
}
}
// test output
echo '<pre>';
print_r($tags);
Upvotes: 1
Reputation: 2381
Try this:
$ar = explode(", ", $s);
for ($i = 0; $i < count($ar); $i++) {
if (strpos($ar[$i], ",")) { //if is array formatted
$begin = strpos($ar[$i], "[");
$name = substr($ar[$i], 0, $begin);
$content = substr(substr($ar[$i], $begin + 1), 0, -1);
$newAr = explode(",", $content);
unset($ar[$i]);
$ar[$i][$name] = $newAr;
}
}
$ar = array_values($ar);
Given $s = "p, br, a[href,title], ul, li, em, strong";
, var_dump($ar) results in:
array(7) {
[0]=>
string(1) "p"
[1]=>
string(2) "br"
[2]=>
string(2) "ul"
[3]=>
string(2) "li"
[4]=>
string(2) "em"
[5]=>
string(6) "strong"
[6]=>
array(1) {
["a"]=>
array(2) {
[0]=>
string(4) "href"
[1]=>
string(5) "title"
}
}
}
Upvotes: 1
Reputation: 3327
If you are sure that there always CSV will have schema comma and space, you can still use explode()
with space added:
$tags = explode(', ', $tags);
Second option is using trim()
on each value:
foreach ($tags as &$tag) {
$tag = trim($tag);
}
unset($tag);
If you are reading your CSV from file the best solution will be using fgetcsv()
function.
Upvotes: 2