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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效