I have a program that selects an amount from the chain that has Kč
or CZK
behind it. How do I edit an expression ( pattern) to check if Kč
or CZK
is in front of a number? See string1 and string2:
$string='Rohlík 4,99 Kč 51235';
//$string1='Rohlík CZK 4,99 51235';
//$string2='Rohlík Kč4,99 51235';
$replace = [' ', '.'];
$string = str_replace($replace,"",$string);
$string = str_replace(',',".",$string);
/*Change?*/
$pattern = '/[0-9]*[.]?[0-9]*[Kč,CZK]/';
preg_match($pattern, $string, $matches); // => 4.99 Kč
$string = $matches;
$pattern = '/[0-9]*[.]?[0-9]*/';
preg_match($pattern, $string[0], $matches);
$price = $matches[0];
print_r($price); // => 4.99
Use logical grouping in your pattern to match the label which may come before or after the targeted number (replacing the comma with a dot can be done after this step).
Code: (Demo)
Output:
Pattern Breakdown: