note 35586 deleted from function.preg-match-all by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 12:23:46 +0000 |
| Subject: | note 35586 deleted from function.preg-match-all by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57443@lists.php.net to get a copy of this message | ||
Note Submitter: tyron at tmm@aon.at
----
I used remcomans Regexp to get the needed Information in a more useful format, and it also can parse
all functions in a string:
function ParseFunctions($str) {
$funcs=array();
preg_replace("/\[([^ ]*) ([^\]]*)\]/e",
"array_push(\$funcs,ParsePars('\\1','\\2'))",$str);
return $funcs;
}
function ParsePars($func,$str) {
//all on one line!!!!
$regexp = '/([a-zA-Z0-9]+)[\s]{0,1}=[\s]{0,1}((\')|"|)
((?:(?:\\\\\\2)|(?(3)[^\']|[^"]))*)(\2)(?:\s|$)/U';
preg_match_all($regexp,$str,$matches,PREG_SET_ORDER);
$pars[0]=$func;
foreach($matches as $value)
$pars[$value[1]]=$value[4];
return $pars;
}
example:
$str="[img title='blub' src='bla' oink='oink']
blablub bla bla
[img src='bla' oink='blub']";
$results=ParseFunctions($str);
echo "<pre>";
print_r($results);
will produce:
Array
(
[0] => Array
(
[0] => img
[title] => blub
[src] => bla
[oink] => oink
)
[1] => Array
(
[0] => img
[src] => bla
[oink] => blub
)
)