Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
如何在php中从字体标记中获取文本_Php_Regex_Rss - Fatal编程技术网

如何在php中从字体标记中获取文本

如何在php中从字体标记中获取文本,php,regex,rss,Php,Regex,Rss,目前,我正在从事一个项目,在该项目中,我使用谷歌新闻提要动态显示谷歌新闻。我已经显示了图片、新闻标题,但我无法提取新闻的描述。以下是我正在使用的代码: <?php $url = "https://news.google.com/news?cf=all&hl=en&pz=1&ned=in&q=india+news&output=rss"; // url to parse $rss = simplexml_load_file($url); // XML

目前,我正在从事一个项目,在该项目中,我使用谷歌新闻提要动态显示谷歌新闻。我已经显示了图片、新闻标题,但我无法提取新闻的描述。以下是我正在使用的代码:

<?php
$url = "https://news.google.com/news?cf=all&hl=en&pz=1&ned=in&q=india+news&output=rss"; // url to parse
$rss = simplexml_load_file($url); // XML parser
$title = $rss->channel->item[0]->title; // getting first news title echo $title;
preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i',$rss->channel->item[0]->description,$matches,PREG_SET_ORDER);echo "<pre>";print_r($matches[0][0]); // i am getting the first news title image//now i want to get the description of the news, and for that i am using below code for getting the text between the <font> tag but there is more than one <font> tage. So how can i get the description of the first news.
$pattern = "/<font ?.*>(.*)<\/font>/";preg_match($pattern, $rss->channel->item[0]->description, $matches);echo "<pre>"; print_r($matches[0][0]);
?>
channel->item[0]->title;//获取第一个新闻标题echo$title;
预匹配所有('/]*)[\“\']?[^>]*>/i',$rss->channel->item[0]->description,$matches,预设置顺序);回显“”;打印($matches[0][0]);//我正在获取第一条新闻标题图像//现在我想获取新闻的描述,为此,我使用下面的代码获取标记之间的文本,但有多个标记。因此,如何获取第一条新闻的描述。
$pattern=“/(.*)/”预匹配($pattern,$rss->channel->item[0]->description,$matches);回显“;打印($matches[0][0]);
?>
如果您打印
$rss->channel->item[0]
,您将获得第一幅图像的标题、图像和描述

感谢您的帮助

谢谢

只需使用:

<table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=in&amp;usg=AFQjCNGGty5KP4Coz6rRkvg4GOnvdGX1Jw&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779050953564&amp;ei=ZtLKVvCAOYf_yAPt8JH4Cg&amp;url=http://economictimes.indiatimes.com/industry/energy/power/india-set-to-seal-major-power-deal-in-bangladesh-beating-china/articleshow/51084850.cms"><b><b>India</b> set to seal major power deal in Bangladesh, beating China</b></a><br><font size="-1"><b><font color="#6f6f6f">Economic Times</font></b></font><br><font size="-1">After China&#39;s recent success in pushing development projects in Sri Lanka, a breakthrough in Bangladesh would be welcome news for <b>Indian</b> officials who have long fretted over Beijing&#39;s encroachment on to territory it considers its own back yard.</font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dlwfqfFszEiI06M&amp;authuser=0&amp;ned=in"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font></td></tr></table><pre><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=in&amp;usg=AFQjCNGGty5KP4Coz6rRkvg4GOnvdGX1Jw&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779050953564&amp;ei=ZtLKVvCAOYf_yAPt8JH4Cg&amp;url=http://economictimes.indiatimes.com/industry/energy/power/india-set-to-seal-major-power-deal-in-bangladesh-beating-china/articleshow/51084850.cms"><b><b>India</b> set to seal major power deal in Bangladesh, beating China</b></a><br><font size="-1"><b><font color="#6f6f6f">Economic Times</font></b></font><br><font size="-1">After China&#39;s recent success in pushing development projects in Sri Lanka, a breakthrough in Bangladesh would be welcome news for <b>Indian</b> officials who have long fretted over Beijing&#39;s encroachment on to territory it considers its own back yard.</font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dlwfqfFszEiI06M&amp;authuser=0&amp;ned=in"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font>
您可以继续:

$pattern = "/<font ?.*>(.*)<\/font>/U";
preg_match_all($pattern, $desc , $matches);

print_r($matches);
$pattern=“/(.*)/”;
预匹配($pattern,$desc,$matches);
回声“;
打印($matches[0]);
输出为:




经济时报
在中国最近成功推动斯里兰卡的发展项目后,孟加拉国的一项突破将是印度官员的喜讯,他们长期以来一直为北京侵占其认为是自己后院的领土而烦恼。

经济时报
在中国最近的成功后在推动斯里兰卡的发展项目方面,孟加拉国的一项突破将是印度官员的喜讯,他们长期以来一直为北京侵占其认为是自己后院的领土而烦恼。

如果你想把它分成更多的碎片

$pattern=“/(.*)/U”;
preg_match_all($pattern,$desc,$matches);
打印(匹配项);

为什么要混合使用正则表达式和dom解析?在这种情况下,单独解析就足够了。@Jan我如何使用dom解析获得第一条新闻的描述,你有什么建议吗。嘿,Alex,我只想要新闻描述,不想要标题和图片,上面的代码给出的不仅仅是我想要的新闻描述。添加更新。虽然脚本确实获得(仅)描述,你可能想测试最后一个preg_match_all,因为它生成了更多的元素,但其中一些元素可能是你所需要的。如果它仍然不是你想要的,你能用预期的输出更新问题吗?你好,Alex,是的,它解决了我的问题,我把它分成了更多的部分,但它仍然没有给出描述,那么我就没有了他通过jQuery selector.lh>font:nth child(5)进行了描述,这对我来说很有用。感谢您在这个问题上为我提供宝贵的帮助。
<table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=in&amp;usg=AFQjCNGGty5KP4Coz6rRkvg4GOnvdGX1Jw&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779050953564&amp;ei=ZtLKVvCAOYf_yAPt8JH4Cg&amp;url=http://economictimes.indiatimes.com/industry/energy/power/india-set-to-seal-major-power-deal-in-bangladesh-beating-china/articleshow/51084850.cms"><b><b>India</b> set to seal major power deal in Bangladesh, beating China</b></a><br><font size="-1"><b><font color="#6f6f6f">Economic Times</font></b></font><br><font size="-1">After China&#39;s recent success in pushing development projects in Sri Lanka, a breakthrough in Bangladesh would be welcome news for <b>Indian</b> officials who have long fretted over Beijing&#39;s encroachment on to territory it considers its own back yard.</font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dlwfqfFszEiI06M&amp;authuser=0&amp;ned=in"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font></td></tr></table><pre><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=in&amp;usg=AFQjCNGGty5KP4Coz6rRkvg4GOnvdGX1Jw&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779050953564&amp;ei=ZtLKVvCAOYf_yAPt8JH4Cg&amp;url=http://economictimes.indiatimes.com/industry/energy/power/india-set-to-seal-major-power-deal-in-bangladesh-beating-china/articleshow/51084850.cms"><b><b>India</b> set to seal major power deal in Bangladesh, beating China</b></a><br><font size="-1"><b><font color="#6f6f6f">Economic Times</font></b></font><br><font size="-1">After China&#39;s recent success in pushing development projects in Sri Lanka, a breakthrough in Bangladesh would be welcome news for <b>Indian</b> officials who have long fretted over Beijing&#39;s encroachment on to territory it considers its own back yard.</font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dlwfqfFszEiI06M&amp;authuser=0&amp;ned=in"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font>
$pattern = "/<font ?.*>(.*)<\/font>/U";
preg_match_all($pattern, $desc , $matches);

print_r($matches);