I'm looking to analyse strings in PHP. They appear as such:
Premium Upgrade (€10.00)
I want to detect when a string contains '(€10.00)', however the price will change. Hence I'm guessing a RegEx for 'Bracket, Float, Bracket' The end result is I want to remove the price from the string, however, its possible something else may appear in the brackets e.g.
Premium Upgrade (Per Person)
And hence I can't just explode or substring on the first opening brackets. However, there could be two, or more, brackets e.g.
Premium Upgrade (Per Person) (€10.00)
Hence in this instance, I would need to output:
Premium Upgrade (Per Person)
Thus my high level flow would be:
- Check string, does it contact brackets and float?
- Yes, remove the brackets and float, leaving other brackets in place, proceed.
- No brackets and float? Leave as is, proceed.
My pysedo code is:
//Get my string
$str = $meta->display_key;
//I need a RegEx here to detect my bracket and float e.g. (€X.YY)
if (strpos($str, '(') !== false) {
//Need to remove brackets and float here, but leave all other brackets in place.
$formatted_string = substr($str, 0, strrpos($str, '('));
}
//Do nothing, the string doesn't match my criteria
else $display_key_formatted = $str;