获取评论html dom解析器之间的数据

时间:2017-09-26 06:48:56

标签: php html parsing dom

我试图在2条评论中删除明文。 我在这里看到了其他一些使用下一个兄弟或者得到孩子的帖子,但这些似乎依赖于找到另一个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>

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

ı认为需要jquery;试试这个

DataPoint

答案 4 :(得分:-1)

您可以将jQuery用作:

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