I have this variable- $number
which may contain different types of number format.
Type1: $number=200,000.00 ;
Type2: $number=(200,000.00) ;
Type3: $number=(20.50) ;
If the number has brackets I want to remove the brackets and add a minus sign on its left side.
Example: If the number is $number=(200,000.00) ;
I want to convert it to -200,000.00
or in case of $number=(20.50) ;
I want to convert it to -20.50
but for $number=200,000.00 ;
I want to keep it just the way it is.
I have tried this preg_replace('/[()^\d-]+/', '', $number);
but its not working.
Could you please show me how to get this done?
Thanks :)
You can do as follows
str_replace(array("(",")"),array("-",""),$number);
I think this should do what you want:
if (strpos($number, "(") !== false && strpos($number, ")") !== false)
$number = '-'. trim($number, "()");
Example: http://codepad.org/Yhb80H0v
Your $number
has to be a string to let PHP "know" about the parentheses. Use this code:
$number = "(1,000.2)";
$number = preg_replace('/\(([0-9,.]+)\)/', '-\1', $number);
print $number;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With