Extract a single integer from a string

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

    This functions will also handle the floating numbers

    $str = "Doughnuts, 4; doughnuts holes, 0.08; glue, 3.4";
    $str = preg_replace('/[^0-9\.]/','-', $str);
    $str = preg_replace('/(\-+)(\.\.+)/','-', $str);
    $str = trim($str, '-');
    $arr = explode('-', $str);
    

提交回复
热议问题