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 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型