2017-09-26 48 views
0

我試圖在2條評論之間刮取純文本。 我在這裏看到一些其他的帖子,使用下一個兄弟姐妹或得到孩子,但這些似乎依賴於找到另一個HTML標記。這些數據只是純文本。 Parse between comments in Simple HTML Dom在評論之間獲取數據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>

+0

可以讀取HTML作爲一個長字符串,然後搜索字符串的開始/結束JS註釋標記,只在 – Peter

回答

0

保持簡單,男人。只是將其作爲文本進行處理會更快更簡單。您可以使用strpos函數查找兩個註釋的位置來分割文本。

+0

之間啊,這可能工作得到字符串值! 在字符串中找到字符串'<! - Data starts here - >'並存在於位置2567處字符串'


'在字符串中找到並存在於位置2664 現在我只是得到那些職位之間的東西! – jointtech

-1

我認爲需要jQuery;試試這個

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

這是一個PHP問題。不,你不需要jQuery。 – JJJ

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 
-1

您可以使用jQuery這個爲:

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

真難看,但是這對我的作品。感謝Gedrox推動正確的方向。

要測試哈坎庫魯答案,看起來像它也會工作。

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