PHP:获取网站的特定内容

PHP:获取网站的特定内容,php,file,web,Php,File,Web,我想将网站的特定内容放入数组中 我有大约20个站点以我喜欢的其他方式获取内容和输出。只有端口总是在变化(不是27015,而是27016左右…) 现在,我在PHP中使用这段代码来获取游戏图标“cs.png”,但图标的长度不同,所以这不是最好的方法,或者:-/ $srvip = '148.251.78.214'; $srvlist = array('27015'); foreach ($srvlist as $srvport) { $source = file_get_contents(

我想将网站的特定内容放入数组中

我有大约20个站点以我喜欢的其他方式获取内容和输出。
只有端口总是在变化(不是27015,而是27016左右…)



现在,我在PHP中使用这段代码来获取游戏图标“cs.png”,但图标的长度不同,所以这不是最好的方法,或者:-/

$srvip = '148.251.78.214';
$srvlist = array('27015');
foreach ($srvlist as $srvport) {
    $source = file_get_contents('http://www.gametracker.com/server_info/'.$srvip.':'.$srvport.'/');  
    $content = array(
                   "icon" => substr($source, strpos($source, 'game_icons64')+13, 6),
               );
    echo $content[icon];
}

感谢您的帮助,我上一次的PHP工作已经过去了几天:p

您只需要查找
游戏图标64
之后的第一个
,然后一直读到那里

$srvip = '148.251.78.214';
$srvlist = array('27015');
foreach ($srvlist as $srvport) {
    $source = file_get_contents('http://www.gametracker.com/server_info/'.$srvip.':'.$srvport.'/');  

    // find the position right after game_icons64/
    $first_occurance = strpos($source, 'game_icons64')+13;

    // find the first occurance of " after game_icons64, where the src ends for the img
    $second_occurance = strpos($source, '"', $first_occurance);

    $content = array(
                  // take a substring starting at the end of game_icons64/ and ending just before the src attribute ends
                   "icon" => substr($source, $first_occurance, $second_occurance-$first_occurance),
               );
    echo $content['icon'];
} 
另外,由于使用了
[icon]
而不是
['icon']


编辑以匹配涉及多个字符串的第二个请求

$srvip = '148.251.78.214';
$srvlist = array('27015');

$content_strings = array( );

// the first 2 items are the string you are looking for in your first occurrence and how many chars to skip from that position
// the third is what should be the first char after the string you are looking for, so the first char that will not be copied
// the last item is how you want your array / program to register the string you are reading
$content_strings[] = array('game_icons64', 13, '"', 'icon');
// to add more items to your search, just copy paste the line above and change whatever you need from it

foreach ($srvlist as $srvport) {
    $source = file_get_contents('http://www.gametracker.com/server_info/'.$srvip.':'.$srvport.'/');  

    $content = array();

    foreach($content_strings as $k=>$v)
    {
        $first_occurance = strpos($source, $v[0])+$v[1];

        $second_occurance = strpos($source, $v[2], $first_occurance);

        $content[$v[3]] = substr($source, $first_occurance, $second_occurance-$first_occurance);
    }

    print_r($content);
} 

这仅仅是你想要的图像吗?你不能直接从源代码中得到它吗?我不确定我是否明白你的意思。我想要源代码的具体内容,例如图像名称,它的长度不同于playercount、playermax、servername……但是我想如果我只得到一些关于图像名称的帮助,我可以自己得到其余的。这个游戏还在玩吗?现在已经15岁了。当然-这是一个简单的老学校:P@StéphaneBruckert它仍然有很多比赛正在举行,更新和补丁即将发布,还有一个庞大的粉丝群…是的,仍然在播放。有可能减少代码吗?因为我有大约10个我想得到的内容字符串,但我有20个vars和$first…和$second…那么,如何将其用于不同的字符串?@am1看一下第二段代码。你可以添加任意多的字符串来查找,它们最终都会形成一个数组。是的,好吧-但这看起来非常专业:)现在如何添加第二个搜索字符串?凯,但这看起来非常专业:)现在如何添加第二个搜索字符串g?@am1好的,假设您还想获取服务器的名称/标题(处理您的示例)。在第一个
content\u strings[]之后,
只需添加行
$content\u strings[]=array('blocknewheadertile',21',,'title'));
。这将查找第一次出现的blocknewheadertitle,跳到span文本的开头,读取到span文本的结尾。它将把信息保存为数组中的标题