src/Str.php
public static function toNumber(string $string, bool $allowDecimal = false, bool $allowNegative = false): string
    {
        $string = trim($string);

        $decimalPart = '';
        if (($firstPos = strpos($string, '.')) !== false) {
            $integerPart = substr($string, 0, $firstPos);
            if ($allowDecimal) {
                $rawDecimalPart = substr($string, $firstPos);
                $filteredDecimalPart = rtrim(preg_replace('/[^0-9]/', '', $rawDecimalPart), '0');
                if (!empty($filteredDecimalPart)) {
                    $decimalPart = '.' . $filteredDecimalPart;
                }
            }
        } else {
            $integerPart = $string;
        }

        $integerPart = ltrim(preg_replace('/[^0-9]/', '', $integerPart), '0');
        $integerPart = $integerPart ?: '0';

        $minusSign = '';
        if (strpos($string, '-') === 0) {
            if (!$allowNegative) {
                return '0';
            } elseif (!($integerPart === '0' && empty($decimalPart))) {
                $minusSign = '-';
            }
        }

        $number = $minusSign . $integerPart . $decimalPart;

        return $number;
    }
public static function toNumber(string $string, bool $allowDecimal = false, bool $allowNegative = false): string
    {
        $string = trim($string);

        $decimalPart = '';
        if (($firstPos = strpos($string, '.')) !== false) {
            $integerPart = substr($string, 0, $firstPos);
            if ($allowDecimal) {
                $rawDecimalPart = substr($string, $firstPos);
                $filteredDecimalPart = rtrim(preg_replace('/[^0-9]/', '', $rawDecimalPart), '0');
                if (!empty($filteredDecimalPart)) {
                    $decimalPart = '.' . $filteredDecimalPart;
                }
            }
        } else {
            $integerPart = $string;
        }

        $integerPart = ltrim(preg_replace('/[^0-9]/', '', $integerPart), '0');
        $integerPart = $integerPart ?: '0';

        $minusSign = '';
        if (strpos($string, '-') === 0) {
            if (!$allowNegative) {
                return '0';
            } elseif (!($integerPart === '0' && empty($decimalPart))) {
                $minusSign = '-';
            }
        }

        $number = $minusSign . $integerPart . $decimalPart;

        return $number;
    }
public static function toNumber ( string $ string , bool $ allowDecimal = false , bool $ allowNegative = false ) : string { $ string = trim ( $ string ) ; $ decimalPart = '' ; if ( ( $ firstPos = strpos ( $ string , '.' ) ) !== false ) { $ integerPart = substr ( $ string , 0 , $ firstPos ) ; if ( $ allowDecimal ) { $ rawDecimalPart = substr ( $ string , $ firstPos ) ; $ filteredDecimalPart = rtrim ( preg_replace ( '/[^0-9]/' , '' , $ rawDecimalPart ) , '0' ) ; if ( ! empty ( $ filteredDecimalPart ) ) { $ decimalPart = '.' . $ filteredDecimalPart ; } } } else { $ integerPart = $ string ; } $ integerPart = ltrim ( preg_replace ( '/[^0-9]/' , '' , $ integerPart ) , '0' ) ; $ integerPart = $ integerPart ? : '0' ; $ minusSign = '' ; if ( strpos ( $ string , '-' ) === 0 ) { if ( ! $ allowNegative ) { return '0' ; } elseif ( ! ( $ integerPart === '0' && empty ( $ decimalPart ) ) ) { $ minusSign = '-' ; } } $ number = $ minusSign . $ integerPart . $ decimalPart ; return $ number ; }
Filter out all non numeric characters and return a clean numeric string.

@param string $string
@param bool   $allowDecimal
@param bool   $allowNegative
@return string
Filter out all non numeric characters and return a clean numeric string .