Extract a single integer from a string

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

    If you just want to filter everything other than the numbers out, the easiest is to use filter_var:

    $str = 'In My Cart : 11 items';
    $int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
    

提交回复
热议问题