I am looking for a way to find a pattern in a PHP string. I am aware of preg_match function
but that only returns a 1 or 0.
I have some deals like these
'5% Off Baby Stationery'
'$10 Off Orders Over $50'
'$5 Off Orders Over $30'
'$7.50 Off Orders Over $40'
'5% Off Your Order'
'11% Off Any Purchase'
'$5 Off Orders Over $50'
'10% Off All Orders'
'$10 Off Orders Over $100'
'$3 Shipping With Any Purchase'
What I am trying to do is get $x or $xx or $xxx or x% or xx% or xxx% from the deal texts.
I want to get the actual values rather than checking if the pattern exists.
For example, from
'$10 Off Orders Over $100',
I want to get $10 and $100 and save them in a variable.
The function preg_match_all()
can be used to store the matches to another variable (third parameter of the function).
$str = '$10 Off Orders Over $100';
$matches = [];
$pattern = '/\$[0-9]*\.{0,1}[0-9]+/';
preg_match_all($pattern, $str, $matches);
echo $matches[0][0]; // $10
echo $matches[0][1]; // $100