Reputation: 1161
Sorry for asking this simple question, but I seem not to find any answers.
How can you check whether there is a number within a string?
I've tried:
$string = "this is a simple string with a number 2432344";
if (preg_match('/[^0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
Doesn't seem to work...
Upvotes: 0
Views: 1648
Reputation: 14305
^
will only negate what's in the regex. You don't need to use it.
Upvotes: 0
Reputation: 225291
If you want to find a number, don't negate the character set with ^
in your regular expression. That means "match anything except a number".
$string = "this is a simple string with a number 2432344";
if (preg_match('/[0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
Also, you could just use \d
instead of [0-9]
, and $string
instead of "$string"
.
Upvotes: 9