【发布时间】:2017-01-07 04:07:36
【问题描述】:
我正在尝试解析以下代码:
<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable" style="table-layout: fixed;max-width:100% !important;width: 100% !important;min-width: 100% !important;">
基本上我需要的是:
Array
(
[0] => <table
[1] => border="0"
[2] => cellpadding="0"
[3] => cellspacing="0"
[4] => height="100%"
[5] => width="100%"
[6] => id="bodyTable"
[7] => style="table-layout: fixed;max-width:100% !important;width: 100% !important;min-width: 100% !important;
)
但是,当使用简单的 explode(" ", $str); 执行此操作时,我的“样式”也被拆分了,是否有一种简单的方法来解析并循环遍历它?
【问题讨论】:
-
如果您在
explode()中使用空格作为分隔符,请从style属性中删除空格。 -
如果您只是在寻找一个快速破解的解决方案,您也可以尝试正则表达式。
-
@kooKooDoneSlapHisWife 你会如何建议这样做?我遇到的另一个问题是可能有一个 class="v1 v2.." 我想保留但按正确的顺序爆炸。
-
@SauliusAntanavicius 使用
preg_match_all和这个正则表达式:regex101.com/r/rR1bX0/2 ...我不宽恕这个,但我无法抗拒:)