【问题标题】:Get data between comments html dom parser获取评论之间的数据 html dom 解析器
【发布时间】:2017-09-26 06:48:55
【问题描述】:

我正在尝试抓取 2 个 cmets 之间的纯文本。 我在这里看到过其他几篇使用下一个兄弟或获取孩子的帖子,但那些似乎依赖于找到另一个 HTML 标记。此数据只是纯文本。 Parse between comments in Simple HTML Dom

HTML Dom Parser 可以吗?

<p clear="both">
<b>Data at: 0620 UTC 26 Sep 2017</b></p>
<!-- Data starts here -->
KSNA 260553Z 00000KT 10SM SCT070 22/08 A2980 RMK AO2 SLP089 FU SCT070 LAST
  T02170083 10261 20211 50006<br /><hr width="65%"/>
<!-- Data ends here -->
</p>

【问题讨论】:

  • 您可以将 HTML 读取为长字符串,然后在字符串中搜索开始/结束 JS 注释标签,并仅获取介于两者之间的字符串值

标签: php html parsing dom


【解决方案1】:

保持简单,伙计。将其作为文本处理会更快更简单。您可以使用strpos 函数通过查找两个 cmets 的位置来对文本进行切片。

【讨论】:

  • 啊,这可能行得通!字符串 '' 在字符串中找到并存在于位置 2567The string '

    ' 在字符串中找到并存在于位置2664 现在我会在这些位置之间找到什么!
【解决方案2】:

如果php那么简单

$sample_data="<!-- Data starts here -->asdasdasdasd<!-- Data starts here -->";
$ayrac="<!-- Data starts here -->" //or '--' whatever
$array_for_dta=explode($ayrac,$sample_data);
foreach($array_for_dta as $val){
    if(empty($val)){continue;}
    $data_val=$data_val.$val;
}
echo $data_val;//asdasdasdasd

【讨论】:

    【解决方案3】:

    真的很难看,但这对我有用。感谢 Gedrox 朝着正确的方向前进。

    要测试 Hakan Kuru 的答案,看起来它也可以。

    <?php 
    $url = 'http://www.aviationweather.gov/metar/data?ids=ksna&format=raw&hours=0&taf=off&layout=off&date=0';
    $content = file_get_contents($url);
    $start   = '<!-- Data starts here -->';
    $end = '<br /><hr width="65%"/>';
    $pos = strpos($content, $start);
    if ($pos === false) {
        echo "The string '$start' was not found in the string ";
    } else {
        echo "The string '$start' was found in the string ";
        echo " and exists at position $pos";
    }
    $pos2 = strpos($content, $end);
    if ($pos2 === false) {
        echo "The string '$end' was not found in the string ";
    } else {
        echo "The string '$end' was found in the string ";
        echo " and exists at position $pos2";
    }
    $count1 = $pos2-$pos;
    echo "count: ";
    echo $count1;
    $posnew = $pos + 25;
    $count1new = $count1 - 25;
    $metartext = substr($content, $posnew, $count1new);
    echo $metartext;
    ?>

    【讨论】:

      【解决方案4】:

      我认为需要 jquery;试试这个

        $('p').each(function(a,aa){
          if($(this).attr('clear')){
              var dt=$(this).text();
              alert(dt);
            }
             });   
      

      【讨论】:

      • 这是一个 PHP 问题。不,你不需要 jQuery。
      【解决方案5】:

      你可以使用 jQuery 作为:

      <script>
          jQuery(document).ready(function(){
              var text = jQuery("p").html();
              alert(text);
              jQuery(".test").html(text);
          });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多