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($マッチ); 以下に戻ります

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

for each ループを書いてみましたが、うまくいきませんでした。

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 に答える