dongpa3109 2012-03-23 13:59
浏览 150
已采纳

PHP和preg_match正则表达式从字符串中删除带小数的数字

Pretty basic stuff here, trying to pull the number 14.5 out of this string using regex and php but I can't seem to get syntax correct. Also, the number is dynamic and may not always be a decimal but the goal here is to try and pull a number between the word Weight: and </li>:

  <ul>
    <li>Manufacturer: something</li>
    <li>Model: 1216D101</li>
    <li>Condition: New</li>
    <li>Dimensions: 12" x 16"</li>
    <li>Sold by the Dozen</li>
    <li>Weight: 14.5 Lbs.</li>
  </ul>

This is what I have so far and have tried variations but keep falling short:

if (preg_match("/\Weight:\(\d+)\.(\d*?)\<\/li>/", $desc, $WEIGHT) == true)
    {  echo $WEIGHT[0];  }
  • 写回答

4条回答 默认 最新

  • dsjuimtq920056 2012-03-23 14:02
    关注

    Try this regex:

    /Weight: ((?:\d+)(?:\.\d*)?)/
    

    The matched number will be available in $WEIGHT[1].

    If you don't want to capture the . in numbers like 123.:

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

报告相同问题?

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积