dongzu0742 2014-09-09 08:59
浏览 21
已采纳

从字符串中抓住美分

I want to grab only the cents from the strings I have, not the full dollar.. anything that starts with 0.xx is what I want to echo out:

$paste = "08/29/2014    Interest Paid       $ 0.03  $ 37.69
08/25/2014  ACH Deposit         $ 0.93  $ 37.66
08/20/2014  ACH Deposit         $ 0.63  $ 36.73
08/04/2014  ACH Deposit         $ 0.95  $ 36.10
08/04/2014  ACH Deposit         $ 0.57  $ 35.15
08/04/2014  ACH Deposit         $ 0.88  $ 34.58
07/31/2014  Interest Paid       $ 0.01  $ 33.70
07/31/2014  ACH Deposit         $ 0.13  $ 33.69
07/31/2014  ACH Deposit         $ 0.96  $ 33.56
07/31/2014  ACH Deposit         $ 0.65  $ 32.60
07/31/2014  ACH Deposit         $ 0.53  $ 31.95
07/31/2014  ACH Deposit         $ 0.83  $ 31.42
07/30/2014  ACH Deposit         $ 0.63  $ 30.59
07/28/2014  ACH Deposit         $ 0.76  $ 29.96";

I have tried the following but it wouldn't do what I need it nor echo line by line of the cents i'm looking for...

$find = strpos($paste, '0.');
sscanf(substr($paste, $find), '0.%d', $numbers);

Any help I can get on this is greatly appreciated. Thank you in advance..

  • 写回答

2条回答 默认 最新

  • douqiang5809 2014-09-09 09:20
    关注

    You could try something like this

    $paste = <<<EOT
    08/29/2014  Interest Paid       $ 0.03  $ 37.69
    08/25/2014  ACH Deposit         $ 0.93  $ 37.66
    08/20/2014  ACH Deposit         $ 0.63  $ 36.73
    08/04/2014  ACH Deposit         $ 0.95  $ 36.10
    08/04/2014  ACH Deposit         $ 0.57  $ 35.15
    08/04/2014  ACH Deposit         $ 0.88  $ 34.58
    07/31/2014  Interest Paid       $ 0.01  $ 33.70
    07/31/2014  ACH Deposit         $ 0.13  $ 33.69
    07/31/2014  ACH Deposit         $ 0.96  $ 33.56
    07/31/2014  ACH Deposit         $ 0.65  $ 32.60 07/31/2014  ACH Deposit         $ 0.53  $ 31.95
    07/31/2014  ACH Deposit         $ 0.83  $ 31.42
    07/30/2014  ACH Deposit         $ 0.63  $ 30.59
    07/28/2014  ACH Deposit         $ 0.76  $ 29.96
    EOT;
    
    preg_match_all('/\$\s00?\.(\d+)/', $paste, $cents);
    print_r($cents[1]);
    

    As you can see, each value doesn't have to be in each line.

    Output:

    Array
    (
        [0] => 03
        [1] => 93
        [2] => 63
        [3] => 95
        [4] => 57
        [5] => 88
        [6] => 01
        [7] => 13
        [8] => 96
        [9] => 65
        [10] => 53
        [11] => 83
        [12] => 63
        [13] => 76
    )
    

    If you want to have the whole value like 0.03 change the pattern above to this.

    \$\s(00?\.\d+)
    

    Working PHP demo | Working Regex demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错