V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
imSam
V2EX  ›  PHP

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

  •  
  •   imSam · 2022-10-07 09:11:00 +08:00 · 1884 次点击
    这是一个创建于 539 天前的主题,其中的信息可能已经有所发展或是发生改变。
    知道要被程序员大佬们笑掉大牙了,但实在是搞不懂了,
    正在做一个网站,希望能通过 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“> ] 呢?谢谢
    22 条回复    2022-10-11 22:32:23 +08:00
    lcy630409
        1
    lcy630409  
       2022-10-07 09:17:27 +08:00   ❤️ 1
    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
        2
    licoycn  
       2022-10-07 09:17:28 +08:00   ❤️ 1
    对结果进行一次`str_replace`
    zh0n9
        3
    zh0n9  
       2022-10-07 09:20:38 +08:00   ❤️ 1
    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
        4
    imSam  
    OP
       2022-10-07 09:22:28 +08:00
    @zh0n9 非常感谢,可以用!
    另感谢上面两位,虽然看不懂但还是感谢。
    Rache1
        5
    Rache1  
       2022-10-07 09:23:20 +08:00   ❤️ 1
    strpos 返回的是搜索字符串出现的首个位置,在你 substr 的时候应该还要加上被搜索字符串的长度。

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

    ```
    Rache1
        12
    Rache1  
       2022-10-07 11:23:32 +08:00
    @func 之前还没注意到过 sscanf 这个函数,看起来不错。
    hlx
        13
    hlx  
       2022-10-07 11:48:47 +08:00
    对你的结果再调用 strip_tags(), 哈哈
    pytth
        14
    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
        15
    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
        16
    buffzty  
       2022-10-07 21:12:23 +08:00   ❤️ 1
    用 php 做爬虫请直接使用正则. 简单高效 别拿字符串函数折磨自己. strpos 我几乎不使用.
    <?php
    $str = file_get_contents('https://steamcommunity.com/app/200210');
    preg_match('~class="apphub_NumInApp"\>(.+?)\</span\>~', $str, $matches);
    echo $matches[1];
    imSam
        17
    imSam  
    OP
       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
        18
    loginv2  
       2022-10-10 14:08:53 +08:00   ❤️ 1
    @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
        19
    imSam  
    OP
       2022-10-10 20:09:07 +08:00
    @loginv2 太感谢了!
    imSam
        20
    imSam  
    OP
       2022-10-10 20:16:50 +08:00
    @loginv2
    貌似又出现问题了。。。就是当 steam 的页面没有<span class="apphub_NumInApp"></span>这一条信息的时候,
    就报错了·~
    Notice: Undefined offset ......
    loginv2
        21
    loginv2  
       2022-10-11 13:02:13 +08:00   ❤️ 1
    <?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
        22
    imSam  
    OP
       2022-10-11 22:32:23 +08:00
    @loginv2 非常感谢,太好用了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5377 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 09:01 · PVG 17:01 · LAX 02:01 · JFK 05:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.