douyan1927 2015-07-24 19:10 采纳率: 0%
浏览 68
已采纳

从没有属性或标签的现场表读取数字

I have a problem. I want to create a script. It should read two numbers from other website but no id and class in div:

<td>
  1 IRR = <b>0.0698</b> Gold 
</td>

How can I read this numbers without any id or class in php? or if it is hard in php how can I do it in js? or ajax?

  • 写回答

1条回答 默认 最新

  • dongtigai3875 2015-07-24 19:22
    关注

    For a server-side (PHP) solution, regular expressions are always a favourite.

    // Your HTML variable
    $html = "<td>
        1 IRR = <b>0.0698</b> Gold 
    </td>";
    
    preg_replace_callback("/<td>(?:[\s
    ]+)?([0-9\.]+) irr = <b>([0-9\.]+)<\/b> gold(?:[\s
    ]+)?<\/td>/im", function($m, $numa, $numb)
    {
        // Your first number is in $numa
        // And your second is in $numb
    }, $html);
    

    For client side (js), you could use jQuery's filter function and a regular expression. It wouldn't be amazing in terms of performance but it would certainly do the trick.

    var regexp = /^([0-9\.]+) irr = ([0-9\.]+) gold$/i;
    
    $('td').filter(function()
    {
        return $(this).text().trim().match(regexp);
    }).each(function()
    {
        var numbers = $(this).text().trim().replace(regexp, '$1,$2').split(',');
    
        // Now `numbers` is an array with the two numbers inside
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取