Extract a single integer from a string

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

    Follow this step it will convert string to number

    $value = '$0025.123';
    $onlyNumeric = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
    settype($onlyNumeric,"float");
    
    $result=($onlyNumeric+100);
    echo $result;
    

    Another way to do it :

    $res = preg_replace("/[^0-9.]/", "", "$15645623.095605659");
    

提交回复
热议问题