66

I know this might sound as really dummy question, but I'm trying to ensure that the provided string is of a number / decimal format to use it later on with PHP's number_format() function.

How would I do it - say someone is typing 15:00 into the text field - what regular expression and php function should I use to remove the colon from it and make it only return the valid characters.

preg_match() returns array - so I can't pass the result to number_format() unless I implode() it or something like this.

Your help would be very much appreciated.

1
  • 123.4.5 or 123:4\5 or abs:4:5 -> what digit do you want in result? Commented Mar 29, 2011 at 15:16

7 Answers 7

166

Using is_numeric or intval is likely the best way to validate a number here, but to answer your question you could try using preg_replace instead. This example removes all non-numeric characters:

$output = preg_replace('/[^0-9]/', '', $string);
Sign up to request clarification or add additional context in comments.

4 Comments

Just put a "." inside the [brackets]. E.g. '/[^.0-9]/' -- note that if it's outside the brackets it will become a wildcard. And, this will cover 90% of your use case but will also allow any order so edge cases like "123." and ".123" will work. A good idea is to test out your regexes against examples you know should or should not fail. A site like this one works great.
Besides the website cited by @buley, another very good one is regex101.com!
i was using intval on big int, and it was giving me max int value which is 2147483647, so this works good for me.,
It's nice to know that in the current day and age one can replace [^0-9] with \D
47

To remove anything that is not a number:

$output = preg_replace('/[^0-9]/', '', $input);

Explanation:

  • [0-9] matches any number between 0 and 9 inclusively.
  • ^ negates a [] pattern.
  • So, [^0-9] matches anything that is not a number, and since we're using preg_replace, they will be replaced by nothing '' (second argument of preg_replace).

Comments

18

This is the right answer

preg_match("/^[0-9]+$/", $yourstr);

This function return TRUE(1) if it matches or FALSE(0) if it doesn't

Quick Explanation :

'^' : means that it should begin with the following ( in our case is a range of digital numbers [0-9] ) ( to avoid cases like ("abdjdf125") )

'+' : means there should be at least one digit

'$' : means after our pattern the string should end ( to avoid cases like ("125abdjdf") )

Comments

7

You can try that one:

$string = preg_replace('/[^0-9]/', '', $string);

Cheers.

Comments

4

Another way to get only the numbers in a regex string is as shown below:

$output = preg_replace("/\D+/", "", $input);

2 Comments

does anyone know where the PHP documentation for the \D special character is? I can only find mention of it in this note: php.net/manual/en/function.preg-replace.php#89364 but would like to find the actual docs for it - thank you
Character d Meaning: Matches a number, equivalent to [0–9]. For example: /d/ or /[0–9]/ matches ‘2’ in “B2 is the suite number.” Character D Meaning: Matches any non-numeric, equivalent to [0-9]. For example: /D/ or /[0-9]/ matches ‘B’ in “B2 is the suite number.” Source: medium.com/@mena.meseha/…
3

use built in php function is_numeric to check if the value is numeric.

3 Comments

I'm using is_numeric - but this only return boolean - so if it's not I'm left with no value. The problem I'm facing here is that during testing of the application someone has typed the colon (:) instead of dot (.) as decimal separator - and this has caused an error with number_format()
123.4.5 -> what digit will be?
is_numeric will return true for -234, -10 and so on.
0

You could do something like this if you want only whole numbers.

function make_whole($v){
    $v = floor($v);
    if(is_numeric($v)){
      echo (int)$v;
      // if you want only positive whole numbers
      //echo (int)$v = abs($v);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.