<SCRIPT> function ShowAttribs(oElem) { txtAttribs.innerHTML = ''; // Retrieve the collection of attributes for the specified object. var oAttribs = oElem.attributes; // Iterate through the collection. for (var i = 0; i < oAttribs.length; i++) { var oAttrib = oAttribs[i]; // Print the name and value of the attribute. // Additionally print whether or not the attribute was specified // in HTML or script. txtAttribs.innerHTML += oAttrib.nodeName + '=' + oAttrib.nodeValue + ' (' + oAttrib.specified + ')<BR>'; } } </SCRIPT>
width
class
style
title
width=str.replace(/(.*)(width=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
class=str.replace(/(.*)(class=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
style=str.replace(/(.*)(style=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
title=str.replace(/(.*)(title=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
function ShowAttribs(oElem)
{
txtAttribs.innerHTML = ''; // Retrieve the collection of attributes for the specified object.
var oAttribs = oElem.attributes; // Iterate through the collection.
for (var i = 0; i < oAttribs.length; i++)
{
var oAttrib = oAttribs[i]; // Print the name and value of the attribute.
// Additionally print whether or not the attribute was specified
// in HTML or script.
txtAttribs.innerHTML += oAttrib.nodeName + '=' +
oAttrib.nodeValue + ' (' + oAttrib.specified + ')<BR>';
}
}
</SCRIPT>
<table width=100 class='c' style='border:1' title="t" >
TEXT;
preg_match_all("/(\w+)=([\"']?)(.+)\\2[\s>]/sU", $s, $r);print_r($r);
<tr><td>
<table width=15 class='c22' style='border:1' title="标题" >
<tr><td>
</td>
</tr>
</table>
</td>
</tr>
</table>
$str = <<<EOT
<table width=100 class='c' style='border:1' title="t" >
EOT;
preg_match_all('|([a-z]+)=([\']?)(.+)\2[\s>]|iU', $str, $arr);
print_r($arr[1]);
?>output:Array
(
[0] => width
[1] => class
[2] => style
[3] => title
)
[0] => Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
),
[1] => Array
(
[4] => width=15
[5] => class='c22'
[6] => style='border:1'
[7] => title="标题"
)
$str = <<<EOT
<table width=100 class='c' style='border:1' title="t" >
EOT;
preg_match_all('|(([a-z]+)=([\']?)(.+)\3)[\s>]|iU', $str, $arr);
print_r($arr[1]);
?>output:Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
)
$str = <<<EOT
<table width=100 class='c' style='border:1' title="t" >
<tr><td>
<table width=15 class='c22' style='border:1' title="标题" >
<tr><td>
</td>
</tr>
</table>
</td>
</tr>
</table>
EOT;
我想得到的结果是:
[0] => Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
),
[1] => Array
(
[4] => width=15
[5] => class='c22'
[6] => style='border:1'
[7] => title="标题"
)目前得到的结果是:
[1] => Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
[4] => width=15
[5] => class='c22'
[6] => style='border:1'
[7] => title="标题"
)
$str = <<<EOT
<table width=100 class='c' style='border:1' title="t" >
<tr><td>
<table width=15 class='c22' style='border:1' title="标题" >
<tr><td>
</td>
</tr>
</table>
</td>
</tr>
</table>
EOT;
preg_match_all('|<table[^>]+>|i', $str, $s);foreach ($s[0] as $v)
{
preg_match_all('|(([a-z]+)=([\']?)(.+)\3)[\s>]|iU', $v, $arr);
$r[] = $arr[1];
}
print_r($r);
?>output:
Array
(
[0] => Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
) [1] => Array
(
[0] => width=15
[1] => class='c22'
[2] => style='border:1'
[3] => title="标题"
))
(?<=\s)([^\s]+?)
得到结果是:
Array
(
[0] => Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
[4] => >
[5] => <tr><td>
[6] => <table
[7] => width=15
[8] => class='c22'
[9] => style='border:1'
[10] => title="标题"
[11] => >
[12] => <tr><td>
[13] => </td>
[14] => </tr>
[15] => </table>
[16] => </td>
[17] => </tr>
[18] => </table>
) [1] => Array
(
[0] => width=100
[1] => class='c'
[2] => style='border:1'
[3] => title="t"
[4] => >
[5] => <tr><td>
[6] => <table
[7] => width=15
[8] => class='c22'
[9] => style='border:1'
[10] => title="标题"
[11] => >
[12] => <tr><td>
[13] => </td>
[14] => </tr>
[15] => </table>
[16] => </td>
[17] => </tr>
[18] => </table>
))