После того, как вы определили <input …>
, вы можете использовать следующий шаблон для извлечения всех атрибутов (заботясь о разделителях значений (одинарные кавычки, двойные кавычки, пробел)).
<?php
$input = '<input value="joe" type="hidden" name="firstname">';
$attributes = array();
$pattern = "/\s+(?<name>[a-z0-9-]+)=(((?<quotes>['\"])(?<value>.*?)\k<quotes>)|(?<value2>[^'\" ]+))/i";
if (preg_match_all($pattern, $input, $matches, PREG_SET_ORDER)) {
$attributes[$match['name']] = $match['value'] ?: $match['value2'];
}
var_dump($input, $attributes);
приведет к
$attributes = array(
'value' => 'joe',
'type' => 'hidden',
'name' => 'firstname',
)
https://gist.github.com/1289335