gpt4 book ai didi

robots.txt 中的 PHP 数组解析站点地图

转载 作者:行者123 更新时间:2023-12-02 05:21:19 25 4
gpt4 key购买 nike

我在尝试使用 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

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

foreach( $match as $sitemap){

echo $sitemap[1];

}

任何帮助将不胜感激

最佳答案

$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] 数组即可。

关于robots.txt 中的 PHP 数组解析站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com