duanliyi5997 2014-01-01 23:31
浏览 28
已采纳

从字符串中提取数字

I would like to extract a number from a string. The string looks something like this:

"visaObs(31124228);"

Here is my shot at it. It doesn't seem to get any matches:

preg_match('/visaObs((\d+));/s', $attribute, $match);
echo $match[0]; //undefined offset

Any ideas?

  • 写回答

4条回答 默认 最新

  • douzhu5900 2014-01-01 23:34
    关注

    You must escape ( and ):

    preg_match('/visaObs[(](\d+)[)];/s', $attribute, $match);
    

    Example:

    $ cat /tmp/1.php
    <?
    $attribute = "visaObs(31124228);";
    preg_match('/visaObs[(](\d+)[)];/s', $attribute, $match);
    print $match[1];
    ?>
    $ php /tmp/1.php
    31124228
    

    If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题