【问题标题】:Parsing HTML element into parts [duplicate]将HTML元素解析成部分[重复]
【发布时间】: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 ...我不宽恕这个,但我无法抗拒:)

标签: php html


【解决方案1】:

试试这个:

$result = array ();
$str = '<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;">';
$str_arr = explode (' style', $str);
$result = explode (' ', $str_arr[0]);
array_push($result, 'style'.$str_arr [1]);

echo '<pre>';
print_r($result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2021-09-19
    • 2018-10-01
    • 2021-04-16
    相关资源
    最近更新 更多