public function setPrice($price, $format = self::EURO)
    {
        $this->validatePriceFormat($format);

        // sanitize: price is null if value like -1.123E-11 provided
        if (preg_match('/^\-?\d+\.\d+E(\+|\-)\d+$/u', (string) $price)) {
            $this->price = 0;

            return $this;
        }

        if ($format == self::EURO) {
            // remove: all not allowed chars
            $price = preg_replace('/[^0-9,\-\.\+]/', '', $price);

            // sanitize: +/- at end of number
            $price = preg_replace('/^(.*)(\-|\+)$/', '$2$1', $price);

            // sanitize: , in price
            if (mb_strpos($price, ',') !== false) {
                if (preg_match('/,\./', $price)) {
                    $price = str_replace(',', '', $price);
                } else {
                    $price = str_replace('.', '', $price);
                    $price = str_replace(',', '.', $price);
                }
            }

            // convert: to internal int structure
            $price = (float) $price;
            $price = $price * 100;
        } else {
            $price = (float) $price;
        }

        $this->price = (int) round($price);

        return $this;
    }
public function setPrice($price, $format = self::EURO)
    {
        $this->validatePriceFormat($format);

        // sanitize: price is null if value like -1.123E-11 provided
        if (preg_match('/^\-?\d+\.\d+E(\+|\-)\d+$/u', (string) $price)) {
            $this->price = 0;

            return $this;
        }

        if ($format == self::EURO) {
            // remove: all not allowed chars
            $price = preg_replace('/[^0-9,\-\.\+]/', '', $price);

            // sanitize: +/- at end of number
            $price = preg_replace('/^(.*)(\-|\+)$/', '$2$1', $price);

            // sanitize: , in price
            if (mb_strpos($price, ',') !== false) {
                if (preg_match('/,\./', $price)) {
                    $price = str_replace(',', '', $price);
                } else {
                    $price = str_replace('.', '', $price);
                    $price = str_replace(',', '.', $price);
                }
            }

            // convert: to internal int structure
            $price = (float) $price;
            $price = $price * 100;
        } else {
            $price = (float) $price;
        }

        $this->price = (int) round($price);

        return $this;
    }
public function setPrice ( $ price , $ format = self :: EURO ) { $ this -> validatePriceFormat ( $ format ) ; // sanitize: price is null if value like -1.123E-11 provided if ( preg_match ( '/^\-?\d+\.\d+E(\+|\-)\d+$/u' , ( string ) $ price ) ) { $ this -> price = 0 ; return $ this ; } if ( $ format == self :: EURO ) { // remove: all not allowed chars $ price = preg_replace ( '/[^0-9,\-\.\+]/' , '' , $ price ) ; // sanitize: +/- at end of number $ price = preg_replace ( '/^(.*)(\-|\+)$/' , '$2$1' , $ price ) ; // sanitize: , in price if ( mb_strpos ( $ price , ',' ) !== false ) { if ( preg_match ( '/,\./' , $ price ) ) { $ price = str_replace ( ',' , '' , $ price ) ; } else { $ price = str_replace ( '.' , '' , $ price ) ; $ price = str_replace ( ',' , '.' , $ price ) ; } } // convert: to internal int structure $ price = ( float ) $ price ; $ price = $ price * 100 ; } else { $ price = ( float ) $ price ; } $ this -> price = ( int ) round ( $ price ) ; return $ this ; }
Set price after cutting out all unwanted chars.

This method converts (almost) every string into a price.

@param mixed  $price
@param string $format [optional] the type of $price, default ConvertPrice::EURO

@return $this
@throws InvalidArgumentException
Set price after cutting out all unwanted chars .