19

I want to check if a variable has a number in it, I just want to see if there is one I don't care if it has any thing else in it like so:

"abc" - false
"!./#()" - false
"!./#()abc" - false
"123" - true
"abc123" - true
"!./#()123" - true
"abc !./#() 123" -true

There are easy ways of doing this if you want to know that is all numbers but not if it just has one. Thanks for your help.

5 Answers 5

69

You can use the strcspn function:

if (strcspn($_REQUEST['q'], '0123456789') != strlen($_REQUEST['q']))
  echo "true";
else
  echo "false";

strcspn returns the length of the part that does not contain any integers. We compare that with the string length, and if they differ, then there must have been an integer.

There is no need to invoke the regular expression engine for this.

7
  • 10
    I benchmarked this one, it's about 80 percent faster than regular expressions. May 23, 2009 at 15:33
  • 1
    Nice -- I didn't even benchmark it, I just figured that there must be a relatively simple string function I could use instead of pulling out the big hammer (regexps)... May 23, 2009 at 15:42
  • 1
    That function is a new one on me, a pity it has such a horrific name (what does it stand for?) it's so close to it's oppose: strspn. IMO this code is harder to understand than the regex, which takes some doing ;) May 23, 2009 at 17:42
  • Thanks for the information, I have used the functions before but not in this way. I like anything that runs faster!
    – Scott
    May 23, 2009 at 20:24
  • 1
    Thanks @MartinGeisler I really found this function to be useful. However I figured that another function acts in similar fashion. (strpbrk($var, '0123456789') e.g. have attached test case below. Jul 2, 2013 at 7:49
12
$result = preg_match("/\\d/", $yourString) > 0;
4
  • Thanks, I tried it before but it didn't work for some reason.
    – Scott
    May 23, 2009 at 15:26
  • 1
    You might want to escape the backslash properly -- here you are relying on the fact that \d is not a valid escape sequence, and therefore it will be treated as if you had written \\d. May 23, 2009 at 15:29
  • You are right about the escaping (I was not relying on something though, I just simply forgot). Corrected, thanks for the hint.
    – Tomalak
    May 23, 2009 at 16:06
  • @therefromhere: Ironically, this has even been down-voted. Though I would expect the speed advantage of strcspn() not to be relevant in 99% of the use cases.
    – Tomalak
    May 25, 2009 at 17:40
8

Holding on to spirit of @Martin, I found a another function that works in similar fashion.

(strpbrk($var, '0123456789')

e.g. test case

<?php

function a($var) {
    return (strcspn($var, '0123456789') != strlen($var));
}

function b($var) {
    return (strpbrk($var, '0123456789'));
}

$var = array("abc", "!./#()", "!./#()abc", "123", "abc123", "!./#()123", "abc !./#() 123");

foreach ($var as $v) {
    echo $v . ' = ' . b($v) .'<hr />';
}

?>
1
  • it should be the faster solution
    – Luca C.
    Sep 11, 2015 at 12:26
4

This should help you:

$numberOfNumbersFound = preg_match("/[0-9]+/", $yourString);

You could get more out of the preg_match function, so have a look at its manual

1
  • Perfect, then all you need to do is check if( $numberOfNumbersFound > 0 ) Thank You!
    – sMyles
    May 10, 2017 at 18:10
-2

you can use this pattern to test your string using regular expressions:

$isNumeric = preg_match("/\S*\d+\S*/", $string) ? true : false;
4
  • 2
    oops! took a minute to correct the pattern, and BAWNG! got a down vote. you are fast guys! ;P
    – farzad
    May 23, 2009 at 15:28
  • won't work if "asdf 123asdf" and why not using just ([0-9]+)
    – Jet
    May 23, 2009 at 16:24
  • i guess the fastest downvote was from me, but not for the regex but for the ? true : false; (==0 would do the same, if you insist on $isNumeric being a boolean value, however in PHP you could have just assign preg_match result to $isNumeric and then tread $isNumeric as a boolean, sinec anything <> 0 would be true and 0 false. I absolutely hate IF (boolean) THEN TRUE ELSE FALSE sort of code May 23, 2009 at 22:34
  • I tested it and it worked. I copied the same string that you offered: "asdf 123asdf" and it returned "true". which according to sixth example of the question, should have returned true. in regex there are many ways to define a pattern, I'm sure that [0-9]+ is correct, a single \d is correct either. the pattern that I recommended here is correct either. I had my down vote because the moment I published my answer, I missed asterisks in the pattern (I thought so). and as himself says, because MasterPeter did not like the ternary operator I used.
    – farzad
    May 24, 2009 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.