在注释HTML DOM解析器之间获取数据



我试图在2条评论之间刮擦纯文本。我在这里看到了其他几篇文章使用下一个兄弟姐妹或生孩子,但是这些帖子似乎依靠找到另一个HTML标签。这些数据只是纯文本。简单的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>

保持简单,伙计。仅将其作为文本进行处理,它将更快,更简单。您可以通过使用strpos函数找到两个注释的位置来切割文本。

如果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

真的很丑陋,但这对我有用。感谢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;
?>

ı认为需要jQuery;尝试这个

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

您可以使用jQuery为:

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

最新更新