鉴于这个DOM
$html=<<<'EOD'
<div class='container clickable' data-param='{"footer":"<div>Bye</div>","info":"We win"}'>
<img src='a.jpg' />
</div>
<a href='a.html'>The A</a>
<span></span>
<span data-span-param='{"detailTag":"<span class=\"link\">Anything here</span>"}'>
<a></a>
</span>
EOD;
我正在尝试使用此表达式preg_match_all html标记:
$tags = array();
if(preg_match_all('~<\s*[\w]+[^>]*>|<\s*/\s*[\w]+\s*>~im',$html,$matchall,PREG_SET_ORDER)){
foreach($matchall as $m){
$tags[] = $m[0];
}
}
print_r($tags);
该表达式的输出是:
Array
(
[0] => < div class=’container clickable’ data-param='{“footer”:”< div>
[1] => < /div>
[2] => < img src=’a.jpg’ />
[3] => < /div>
[4] => < a href=’a.html’>
[5] => < /a>
[6] => < span>
[7] => < /span>
[8] => < span data-span-param='{“detailTag”:”< span class=\”link\”>
[9] => < /span>
[10] => < a>
[11] => < /a>
[12] => < /span>
)
我的预期输出是这样的:
Array
(
[0] => < div class=’container clickable’ data-param='{“footer”:”< div>Bye< /div>”,”info”:”We win”}’>
[1] => < img src=’a.jpg’ />
[2] => < /div>
[3] => < a href=’a.html’>
[4] => < /a>
[5] => < span>
[6] => < /span>
[7] => < span data-span-param='{“detailTag”:”< span class=\”link\”>Anything here< /span>”}’>
[8] => < a>
[9] => < /a>
[10] => < /span>
)
我需要一个表达式的帮助来解决这个问题.
最佳答案 这将匹配所有html标记,并且不会捕获用双引号或单引号括起来的标记
<?php
$html=<<<EOD
<div class='container clickable' data-param='{"footer"<div>Bye</div>","info":"We win"}'>
<img src='a.jpg' />
</div>
<a href='a.html'>The A</a>
<span></span>
<span data-span-param='{"detailTag":"<span class=\"link\">Anything here</span>"}'>
<a></a>
</span>
EOD;
$html = preg_replace('~\<\;~is','<',$html);
$html = preg_replace('~\>\;~is','>',$html);
//$html = preg_replace('~\"\;~is','"',$html);
$html = preg_replace('~=\s*\'\s*\'~is','=\'.\'',$html);
$html = preg_replace('~=\s*"\s*"~is','="."',$html);
if(preg_match_all('~((?<==\')(?:.(?!\'))*.)\'|((?<==")(?:.(?!"))*.)"~im',$html,$matchall,PREG_SET_ORDER)){
foreach($matchall as $m){
if(preg_match('~\<~is',$m[0],$mtch1)||preg_match('~\>~is',$m[0],$mtch2)){
$end = $m[0][(strlen($m[0])-1)];
$replace1 = substr($m[0],0,(strlen($m[0])-1));
$replace = preg_replace('~"~is','"',$replace1);
$replace = preg_replace('~<~is','<',$replace);
$replace = preg_replace('~>~is','>',$replace);
$html = preg_replace("~".preg_quote(($end.$replace1.$end),'~')."~is",$end.$replace.$end,$html);
}
}
}
$tags = array();
if(preg_match_all('~<\s*[\w]+[^>]*>|<\s*/\s*[\w]+\s*>~im',$html,$matchall,PREG_SET_ORDER)){
foreach($matchall as $m){
$tags[] = $m[0];
}
}
print_r($tags);
?>
输出:
Array
(
[0] => <div class='container clickable' data-param='{"footer":"<div>Bye</div>","info":"We win"}'>
[1] => <img src='a.jpg' />
[2] => </div>
[3] => <a href='a.html'>
[4] => </a>
[5] => <span>
[6] => </span>
[7] => <span data-span-param='{"detailTag":"<span class=\"link\">Anything here</span>"}'>
[8] => <a>
[9] => </a>
[10] => </span>
)