WC_Shipping_Flat_Rate::is_math_expressionprotectedWC 1.0

Check if a value is a math expression.

Matches two or more numeric operands separated by arithmetic operators.

Method of the class: WC_Shipping_Flat_Rate{}

No Hooks.

Returns

true|false. True if value is a well-formed math expression.

Usage

// protected - for code of main (parent) or child class
$result = $this->is_math_expression( $value ): bool;
$value(string) (required)
Value to test.

WC_Shipping_Flat_Rate::is_math_expression() code WC 10.9.4

protected function is_math_expression( string $value ): bool {
	$decimal_separator = wc_get_price_decimal_separator();

	// Use preg_quote() to safely escaping separator.
	$separator = preg_quote( $decimal_separator, '/' );

	// Breakdown:
	// [\d{sep}\s]+             - First operand: digits, separator, or spaces.
	// (                        - Begin repeating group:
	// [+\-*\/]               -   An arithmetic operator (+, -, *, /).
	// [\d{sep}\s]+           -   Next operand: digits, separator, or spaces.
	// )+                       - One or more operator+operand pairs (ensures no trailing operator).
	return (bool) preg_match(
		'/^[\d' . $separator . '\s]+([+\-*\/][\d' . $separator . '\s]+)+$/',
		trim( $value )
	);
}