求教个 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“> ] 呢?谢谢
1932 次点击
所在节点    PHP
22 条回复
lcy630409
2022-10-07 09:17:27 +08:00
http://www.querylist.cc/

use QL\QueryList;
$data = QueryList::get('https://steamcommunity.com/app/200210')->find('.apphub_NumInApp')->texts();
print_r($data->all());


------>
Array
(
[0] => 1,905 In-Game
)
licoycn
2022-10-07 09:17:28 +08:00
对结果进行一次`str_replace`
zh0n9
2022-10-07 09:20:38 +08:00
if ($x=strpos($str,'<span class="apphub_NumInApp">')) $str=substr($str,$x);
改成
if ($x=strpos($str,'<span class="apphub_NumInApp">')) $str=substr($str,$x+30);
imSam
2022-10-07 09:22:28 +08:00
@zh0n9 非常感谢,可以用!
另感谢上面两位,虽然看不懂但还是感谢。
Rache1
2022-10-07 09:23:20 +08:00
strpos 返回的是搜索字符串出现的首个位置,在你 substr 的时候应该还要加上被搜索字符串的长度。

另外,如要使用这种方式处理,建议使用 mb_* 系列函数
Rache1
2022-10-07 09:23:54 +08:00
被搜索字符串的长度 -> 搜索字符串的长度
imSam
2022-10-07 09:28:31 +08:00
@Rache1 谢谢耐心解答,我是个设计,你所说的这些我像在看天书,不过还是很感谢耐心回答。
Juszoe
2022-10-07 09:36:43 +08:00
html 一般用 dom 解析器解析比较好一些
mologo
2022-10-07 10:52:22 +08:00
可以使用正则获取想要的数据
func
2022-10-07 10:55:03 +08:00
```php
<?php

function fetch() {
$url = 'https://steamcommunity.com/app/200210';

$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
CURLOPT_URL => $url,
CURLOPT_HEADER => [
'User-Agent' =>'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
],
]);

return curl_exec($ch);
}

function parseHTML( $html) {
if (!$html) {
return '';
}

libxml_use_internal_errors(true);

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = true;
$dom->loadHTML($html);
$xpath =new DOMXPath($dom);

$node = $xpath->query('//span[@class="apphub_NumInApp"]');
$text = $node->item(0)->textContent;

// 有可能是中文,以空格分隔
list($onlineCount, $left) = sscanf($text, '%s %s');

return str_replace( ',', '', $onlineCount);
}

$result = parseHTML(fetch());

var_dump($result);

```
ragnaroks
2022-10-07 11:10:18 +08:00
Rache1
2022-10-07 11:23:32 +08:00
@func 之前还没注意到过 sscanf 这个函数,看起来不错。
hlx
2022-10-07 11:48:47 +08:00
对你的结果再调用 strip_tags(), 哈哈
pytth
2022-10-07 11:53:33 +08:00
原理:截取某个字符后面的内容

<?php
$zxrs = '<span class=”apphub_NumInApp“>1,910 In-Game';
echo substr($zxrs, strripos($zxrs, "App") + 7);
?>
pytth
2022-10-07 11:57:56 +08:00
如果只想获得 1910 ,那么直接就先截取某个字符后面的内容,再截取 In-Game 前面的内容就得出来了。

<?php
$zxrs = '<span class=”apphub_NumInApp“>1,910 In-Game';
$zxrs_1 = substr($zxrs, strripos($zxrs, "App") + 7);
$zxrs_2 = substr($zxrs_1, 0, strrpos($zxrs_1, " In-Game"));
echo str_replace(",","",$zxrs_2);
?>
buffzty
2022-10-07 21:12:23 +08:00
用 php 做爬虫请直接使用正则. 简单高效 别拿字符串函数折磨自己. strpos 我几乎不使用.
<?php
$str = file_get_contents('https://steamcommunity.com/app/200210');
preg_match('~class="apphub_NumInApp"\>(.+?)\</span\>~', $str, $matches);
echo $matches[1];
imSam
2022-10-08 01:59:20 +08:00
@buffzty 谢谢,但是又出现了问题,就是我把数字替换成我程序里的变量,就会出错。用之前的方法是不会的,不清楚是哪里的问题。

<?php
$str=file_get_contents("https://steamcommunity.com/app/$steamid");
preg_match('~class="apphub_NumInApp"\>(.+?)\</span\>~', $str, $matches);
echo $matches[1];
?>
loginv2
2022-10-10 14:08:53 +08:00
@imSam
<?php
$steamid = '200210';
$str=file_get_contents("https://steamcommunity.com/app/".$steamid);
preg_match('~class="apphub_NumInApp"\>(.+?)\</span\>~', $str, $matches);
echo $matches[1];
?>
imSam
2022-10-10 20:09:07 +08:00
@loginv2 太感谢了!
imSam
2022-10-10 20:16:50 +08:00
@loginv2
貌似又出现问题了。。。就是当 steam 的页面没有<span class="apphub_NumInApp"></span>这一条信息的时候,
就报错了·~
Notice: Undefined offset ......

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

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

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

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

© 2021 V2EX