0

I am trying to scrape the plain text between 2 comments. I have seen a couple other posts here using next sibling or get child but those seem to rely on finding another HTML tag. This data is just plain text. Parse between comments in Simple HTML Dom

Is it possible with 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>

1
  • You can read the HTML as a long string then search the string for the start/end JS comment tags and only get the string value in between Commented Sep 26, 2017 at 6:51

5 Answers 5

0

Keep it simple, man. It will be faster and simpler just to process this as text. You can slice the text by finding the positions of the two comments using the strpos function.

Sign up to request clarification or add additional context in comments.

1 Comment

ah this may work! The string '<!-- Data starts here -->' was found in the string and exists at position 2567The string '<br /><hr width="65%"/>' was found in the string and exists at position 2664 now Ill just get whats in between those positions!!
0

if php then so easy

$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

Comments

0

Really ugly but this works for me. Thanks to Gedrox for the push in the right direction.

Going to test Hakan Kuru answer which looks like it will work as well.

<?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;
?>

Comments

-2

ı think need jquery; try this

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

1 Comment

It's a PHP question. No, you don't need jQuery.
-2

You can use jQuery for this as :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.