求教个 PHP 的 substr 问题 ....

2022-10-07 09:11:00 +08:00
 imSam
知道要被程序员大佬们笑掉大牙了,但实在是搞不懂了,
正在做一个网站,希望能通过 PHP 调用 steam 上某款游戏的实时在线人数,所以在网上找了一段代码:

<?php
$str=file_get_contents('https://steamcommunity.com/app/200210');
if ($x=strpos($str,'<span class="apphub_NumInApp">')) $str=substr($str,$x);
if ($x=strpos($str,'</span>')) $str=substr($str,0,$x);
echo $str;
?>

这样倒是可以调取我要的内容了,但是输出的是

<span class=”apphub_NumInApp“>1,910 In-Game

怎么改才能不要前边的 [ <span class=”apphub_NumInApp“> ] 呢?谢谢
1962 次点击
所在节点    PHP
22 条回复
loginv2
2022-10-11 13:02:13 +08:00
<?php
$steamid = '200210';
$str=file_get_contents("https://steamcommunity.com/app/".$steamid);
preg_match('~class="apphub_NumInApp"\>(.+?)\</span\>~', $str, $matches);
if(isset($matches[1]) && !empty($matches[1])){
echo $matches[1];
}else{
echo '没找到你要的数据';
}
imSam
2022-10-11 22:32:23 +08:00
@loginv2 非常感谢,太好用了!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/884964

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX