Extract a single integer from a string

后端 未结 21 2496
一个人的身影
一个人的身影 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:01

    If you don't know which format the number is? int or floating, then use this :

    $string = '$125.22';
    
    $string2 = '$125';
    
    preg_match_all('/(\d+.?\d+)/',$string,$matches); // $matches[1] = 125.22
    
    preg_match_all('/(\d+.?\d+)/',$string2,$matches); // $matches[1] = 125
    

提交回复
热议问题