PHP 从网页编辑文本


PHP edit text from webpage

目前我有这个:

<?php
$stran = file_get_contents("http://meteo.arso.gov.si/uploads/probase/www/fproduct/text/sl/fcast_si_text.html");
$stran = str_replace("<h2>","'n",$stran);
$stran = str_replace("</h2>","'n",$stran);
$stran = str_replace("<h1>","'n",$stran);
$stran = str_replace("</h1>","'n",$stran);
$stran = strip_tags($stran);
echo $stran;
?>

现在这给了我在顶部的一些空白行。我还想删除"Vir: Državna meteorološka služba RS (meteo.si - ARSO)"之后的所有文本,包括此字符串之前的空行。

我尝试了一些正则表达式,但全部删除所有文本。热我做吗?

可以使用正则表达式来完成。

// Convert h1/h2 opening/closing tags to new line, ignore case
$stran = preg_replace('/<'/?h[12]>/i', "'n", $stran);
$stran = strip_tags($stran);
// Remove all leading whitespace
$stran = preg_replace('/^'s+/', '', $stran);
// Remove everything after "Vir: ..."
$stran = preg_replace('/(?<=Vir: Državna meteorološka služba RS '(meteo.si - ARSO')).*/s', '', $stran);    

一般来说,我建议真正解析 html 以提取信息。看看 http://php.net/manual/en/class.domdocument.php