PHP regular expression - filter number only

PhpRegex

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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 );

Solution 2 - Php

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).

Solution 3 - Php

You can try that one:

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

Cheers.

Solution 4 - Php

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") )

Solution 5 - Php

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

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

Solution 6 - Php

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

Solution 7 - Php

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);
    }
}

Solution 8 - Php

preg_replace("/\D/", '', $value);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser398341View Question on Stackoverflow
Solution 1 - PhpbuleyView Answer on Stackoverflow
Solution 2 - PhpnetcoderView Answer on Stackoverflow
Solution 3 - PhpSileNTView Answer on Stackoverflow
Solution 4 - PhpMadjid BoudisView Answer on Stackoverflow
Solution 5 - PhpSamuel TerraView Answer on Stackoverflow
Solution 6 - PhpHeadshotaView Answer on Stackoverflow
Solution 7 - PhpKyle CootsView Answer on Stackoverflow
Solution 8 - PhpDEV Tiago FrançaView Answer on Stackoverflow