Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用preg_match使用PHP提取内容_Php_Preg Match - Fatal编程技术网

使用preg_match使用PHP提取内容

使用preg_match使用PHP提取内容,php,preg-match,Php,Preg Match,我正在使用preg_match()提取这个-大部分是干的。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。风力减弱(星期三早上从北风吹来的新鲜风,星期三晚上风平浪静)从现在开始-三天天气预报摘要:大部分地区干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。风力减弱(星期三早上从北风吹来的新风,星期三晚上平静)。 我的代码不工作,只返回Array() $contents=“3天天气预报摘要:大部分天气干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。

我正在使用preg_match()提取这个-

大部分是干的。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。风力减弱(星期三早上从北风吹来的新鲜风,星期三晚上风平浪静)


从现在开始-

三天天气预报摘要:大部分地区干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。风力减弱(星期三早上从北风吹来的新风,星期三晚上平静)。



我的代码不工作,只返回
Array()


$contents=“3天天气预报摘要:大部分天气干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。
风力减弱(星期三早上来自北风的清新风,星期三晚上平静)。”;

preg_match('/3天天气预报摘要:(.*)例如,您想匹配“Robert Got.”中的“Robert Got.我从来没有意识到什么时候去了。
。您应该按以下方式使用
preg_match()

$text = "<span style='color: #999;'>Robert Went.I never realized when.<br />";

$matches = array();

preg_match("/.*(Robert Went\.).*/", $text, $matches);
$text=“罗伯特去了。我从没意识到什么时候去的。
”; $matches=array(); preg_match(“/.*(Robert Gone\).*/”,$text,$matches);
我想说是你的朋友,但如果你真的想用preg_match解决这个问题,你可以试试这个:

$contents = "3 Day Weather Forecast Summary:<\/b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).</span>";

preg_match( '@<span class="phrase">(.*?)</span>@s', $contents, $matches);

var_export( $matches );
$contents=“3天天气预报摘要:大部分天气干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。
风力减弱(星期三早上来自北风的清新风,星期三晚上平静)。”;
preg_match('@(.*)@s',$contents,$matches);
var_导出(匹配项);
更新:

如果您不能参加课程,请尝试以下方法:

preg_match( '@3 Day Weather Forecast Summary:.*?<span class="read-more-content"> <span class="phrase">(.*?)</span>@s', $contents, $matches);
preg_match(“@3天天气预报摘要:.*(.*?@s)”,$contents,$matches);
输出将是:

Array
(
    [0] => <span class="phrase">Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).</span>
    [1] => Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).
)
数组
(
[0]=>大部分干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。
风力减弱(星期三早上来自北风的新鲜风,星期三晚上平静)。
[1] =>大部分干燥。非常温和(周五下午最高16摄氏度,周二晚上最低12摄氏度)。
风力减弱(星期三早上来自北风的新鲜风,星期三晚上平静)。
)

您似乎只是从字符串(以及第一部分“3天天气预报摘要”)中删除了所有html代码(
span
标记)。为什么不检测所有
?类似于:

$text = preg_replace('/<.*?>/', '', $text);
$text = trim(substr($text, strlen('3 Day Weather Forecast Summary:')));
$text=preg_replace(“//”、“$text”);
$text=trim(substr($text,strlen('3天天气预报摘要:'));
第一行用空字符串替换
(包括)中的所有文本。
用于使其不贪婪,以便仅删除匹配的

第二行只是删除了前导字符串。因为它可能有前导空格,也可能没有前导空格,我还包括了
trim
函数,但这可能不是必需的


显然,这两行也可以合并成一行。

我的匹配短语不会是静态的。它会是动态的,并且会在
3天天气预报摘要:
匹配短语
实际上我正在抓取一个网页,因此不能使用类,因为它们可能是多余的。我必须使用“3天天气预报摘要”的唯一性。
$text = preg_replace('/<.*?>/', '', $text);
$text = trim(substr($text, strlen('3 Day Weather Forecast Summary:')));