note 88963 deleted from function.preg-match-all by danbrown
| From: | danbrown@php.net | Date: | Sun, 15 Feb 2009 21:53:27 +0000 |
| Subject: | note 88963 deleted from function.preg-match-all by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150711@lists.php.net to get a copy of this message | ||
Note Submitter: jake at i12 dot com
----
I've been using the following to extract the attributes from a tag ...
<?php
//initiatlise test tag
$tag='<input type = "checkbox" checked space="ok with spaces">';
//generate key=>value array with first item as tag name
preg_match_all('/(\w+)(?:\s*=\s*"([^"]+)"|)/', $tag, $result);
$attributes=array_combine($result[1],$result[2]);
//output attribute array
echo '<pre>'.print_r($attributes,true).'</pre>';
?>
The above example produces the following array:
Array ( [input] => [type] => checkbox [checked] => [space] => ok with spaces)
where the first item is the tag name and rest are the attribues and value.
It can't cope without quotes so if anyone can make a concise suggestion it would be very
helpfull!
i.e. name=test or [color=blue doesn't work but name="test" and
[color="blue" does work.