2

我在尝试使用 php 从数组中获取 url 数据时遇到一些问题。

我的代码是,我试图获取 robots.txt 文件中提到的每个站点地图

$robots_file = file_get_contents($robotsTXT);
$pattern = "/Sitemap: ([^\r\n]*)/";
$i = preg_match_all($pattern, $robots_file, $match, PREG_SET_ORDER);

print_r($match);

print_r($match); 返回下方

Array ( 
    [0] => Array ( [0] => Sitemap: http://www.google.com/culturalinstitute/sitemap.xml 
    [1] => http://www.google.com/culturalinstitute/sitemap.xml ) 
    [1] => Array ( [0] => Sitemap: http://www.google.com/hostednews/sitemap_index.xml 
    [1] => http://www.google.com/hostednews/sitemap_index.xml ) 
    [2] => Array ( [0] => Sitemap: http://www.google.com/sitemaps_webmasters.xml 
    [1] => http://www.google.com/sitemaps_webmasters.xml ) 
    [3] => Array ( [0] => Sitemap: http://www.google.com/ventures/sitemap_ventures.xml 
    [1] => http://www.google.com/ventures/sitemap_ventures.xml ) 
    [4] => Array ( [0] => Sitemap: http://www.gstatic.com/dictionary/static/sitemaps/sitemap_index.xml [1] => http://www.gstatic.com/dictionary/static/sitemaps/sitemap_index.xml ) 
    [5] => Array ( [0] => Sitemap: http://www.gstatic.com/earth/gallery/sitemaps/sitemap.xml 
    [1] => http://www.gstatic.com/earth/gallery/sitemaps/sitemap.xml ) 
    [6] => Array ( [0] => Sitemap: http://www.gstatic.com/s2/sitemaps/profiles-sitemap.xml 
    [1] => http://www.gstatic.com/s2/sitemaps/profiles-sitemap.xml ) 
    [7] => Array ( [0] => Sitemap: http://www.gstatic.com/trends/websites/sitemaps/sitemapindex.xml 
    [1] => http://www.gstatic.com/trends/websites/sitemaps/sitemapindex.xml )
) 

我想要做的是像这样显示地址

http://www.google.com/culturalinstitute/sitemap.xml
http://www.google.com/hostednews/sitemap_index.xml
http://www.google.com/sitemaps_webmasters.xml 
http://www.google.com/ventures/sitemap_ventures.xml
http://www.gstatic.com/dictionary/static/sitemaps/sitemap_index.xml
http://www.gstatic.com/earth/gallery/sitemaps/sitemap.xml 
http://www.gstatic.com/s2/sitemaps/profiles-sitemap.xml
http://www.gstatic.com/trends/websites/sitemaps/sitemapindex.xml

我尝试为每个循环编写一个,但我无法让它工作。

foreach( $match as $sitemap){

echo $sitemap[1];

}

任何帮助,将不胜感激

4

2 回答 2

3
$robots_file = file_get_contents($robotsTXT);

$pattern = '/Sitemap: ([^\s]+)/';
preg_match_all($pattern, $robots_file, $match);

print_r($match[1]);

foreach ($match[1] as $sitemap)
{
    echo $sitemap . "<br />\n";
}

您不需要遍历整个匹配的数组,只需要遍历 $match[1] 的数组。

于 2012-12-10T01:02:06.073 回答
2

而不是echo $sitemap;尝试echo $sitemap[1];

于 2012-12-10T00:55:48.960 回答