I've currently got
preg_replace('/[^0-9]/s', '', $myvariable);
For example the input is GB -3. My current line is giving me the value 3. I want to be able to keep the minus so that the value shows -3 instead of the current output of 3.
You can use preg_replace in this case; $res = preg_replace("/[^0-9]/", "", "Every 6 Months" );
The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a replacement string.
PHP | preg_replace() Function The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
try this :
preg_replace('/[^\d-]+/', '', $myvariable);
breakdown of regex:
//
on both sides are regex delimiter - everything that is inside is regex[]
means that this is character class. Rules change in character class
^
inside character class stands for "not".\d
is short for [0-9], only difference is that it can be used both inside and outside of character class-
will match minus sign+
in the end is the quantifier, it means that this should match one or more charactersIf 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