views:

1681

answers:

5

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.

+3  A: 
$result = preg_match("/\\d/", $yourString) > 0;
Tomalak
Thanks, I tried it before but it didn't work for some reason.
Scott
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.
Martin Geisler
You are right about the escaping (I was not relying on something though, I just simply forgot). Corrected, thanks for the hint.
Tomalak
+1 because it's easier to read than the strcspn version
therefromhere
@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
A: 

See Tomalak's answer.

You might want to check out Regular Expressions in PHP so you understand what is going on.

Jesse Dearing
+10  A: 

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.

Martin Geisler
I benchmarked this one, it's about 80 percent faster than regular expressions.
The Pixel Developer
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)...
Martin Geisler
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 ;)
therefromhere
Thanks for the information, I have used the functions before but not in this way. I like anything that runs faster!
Scott
I believe the name strcspn is a short for "string complement span" or something like that, and what they really mean is "string length of complement span" :-)
Martin Geisler
Very good recommendation!!
Alejandra
A: 

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

$isNumeric = preg_match("/\S*\d+\S*/", $string) ? true : false;
farzad
oops! took a minute to correct the pattern, and BAWNG! got a down vote. you are fast guys! ;P
farzad
won't work if "asdf 123asdf"and why not using just ([0-9]+)
Jet
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
Peter Perháč
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
+1  A: 

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

Peter Perháč