Extract a single integer from a string

后端 未结 21 2473
一个人的身影
一个人的身影 2020-11-21 23:33

I want to extract the digits from a string that contains numbers and letters like:

"In My Cart : 11 items"

I want to extract the nu

21条回答
  •  无人共我
    2020-11-22 00:09

    I do not own the credit for this, but I just have to share it. This regex will get numbers from a string, including decimal points/places, as well as commas:

    /((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/

    Cited from here:
    php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

提交回复
热议问题