is_numeric() tests whether a value is a number. It doesn't necessarily have to be an integer though - it could a decimal number or a number in scientific notation.
The preg_match() example you've given only checks that a value contains the digits zero to nine; any number of them, and in any sequence.
Note that the regular expression you've given also isn't a perfect integer checker, the way you've written it. It doesn't allow for negatives; it does allow for a zero-length string (ie with no digits at all, which presumably shouldn't be valid?), and it allows the number to have any number of leading zeros, which again may not be the intended.
[EDIT]
As per your comment, a better regular expression might look like this:
/^[1-9][0-9]*$/
This forces the first digit to only be between 1 and 9, so you can't have leading zeros. It also forces it to be at least one digit long, so solves the zero-length string issue.
You're not worried about negatives, so that's not an issue.
You might want to restrict the number of digits, because as things stand, it will allow strings that are too big to be stored as integers. To restrict this, you would change the star into a length restriction like so:
/^[1-9][0-9]{0,15}$/
This would allow the string to be between 1 and 16 digits long (ie the first digit plus 0-15 further digits). Feel free to adjust the numbers in the curly braces to suit your own needs. If you want a fixed length string, then you only need to specify one number in the braces.
Answer from Spudley on Stack Overflowis_numeric() tests whether a value is a number. It doesn't necessarily have to be an integer though - it could a decimal number or a number in scientific notation.
The preg_match() example you've given only checks that a value contains the digits zero to nine; any number of them, and in any sequence.
Note that the regular expression you've given also isn't a perfect integer checker, the way you've written it. It doesn't allow for negatives; it does allow for a zero-length string (ie with no digits at all, which presumably shouldn't be valid?), and it allows the number to have any number of leading zeros, which again may not be the intended.
[EDIT]
As per your comment, a better regular expression might look like this:
/^[1-9][0-9]*$/
This forces the first digit to only be between 1 and 9, so you can't have leading zeros. It also forces it to be at least one digit long, so solves the zero-length string issue.
You're not worried about negatives, so that's not an issue.
You might want to restrict the number of digits, because as things stand, it will allow strings that are too big to be stored as integers. To restrict this, you would change the star into a length restriction like so:
/^[1-9][0-9]{0,15}$/
This would allow the string to be between 1 and 16 digits long (ie the first digit plus 0-15 further digits). Feel free to adjust the numbers in the curly braces to suit your own needs. If you want a fixed length string, then you only need to specify one number in the braces.
According to http://www.php.net/manual/en/function.is-numeric.php, is_numeric alows something like "+0123.45e6" or "0xFF". I think this not what you expect.
preg_match can be slow, and you can have something like 0000 or 0051.
I prefer using ctype_digit (works only with strings, it's ok with $_GET).
<?php
_GET['id'];
if (ctype_digit($id)) {
echo 'ok';
} else {
echo 'nok';
}
?>
Corrected syntax:
$regex="/^[0-9,]+$/";
^ represents start of line
+ represents one or more of the group characters
$ represents end of line
This should do it:
'~^\d+(,\d+)?$~'
It allows e.g. 1 or 11,5 but fails on 1, or ,,1 or ,,
^Start of\d+followed by one or more digits(,\d+)?optional: comma,followed by one or more digits$end
\d is a shorthand for digit [0-9]
You asked what's wrong with $regex="/[0-9,]/";
It would match any 0-9 or , which are in the [characterclass]. Even, when matching a string like abc1s or a,b because no anchors are used.
Validating a phone number isn't quite as simple as has so far been suggested. Not only do you have to check that the phone number only contains numbers you also have to make sure that it is the correct length. You also need to make sure that you don't make it difficult for the end user to... use... otherwise you'll end up losing customers.
Example:
If I enter my phone number as 09999 999 999 it won't validate with your system. Because it contains spaces a lot of people do enter phone numbers this way and other, more complex, ways.
Code
if(empty($_REQUEST['phone'])){
//Empty phone number
}
else if(preg_match('/^(0\d{10}|[1-9]\d{9})$/', $_REQUEST['phone'], $matches)){
//Good phone number
}
else{
//Bad phone number
}
Regex Explained
^- Start of string(- Start a capture group0\d{10}- Match a 0 followed by an additional 10 numbers|- ...OR...[1-9]\d{9}- Match a non-0 number followed by 9 other numbers)- Close capture group$- Match end of string
Additional Checks
People often add spaces or brackets/punctuation to make a phone number easier to read/remember for a human. Some examples of phone number input might be:
- (00000) 999 999
- (0)9999 999 999
- 09999 999 999
- 09999999999
- 9999999999
These would all be valid phone numbers but wouldn't be accepted by your system...
To fix this (and make life for users much easier) you would remove characters from the phone number that aren't numbers and then check it for length.
$phoneNumber = "(09999) 999 999";
$phoneNumber = preg_replace('/[^\d]/', '', $phoneNumber); //Replace non-numbers with nothing
echo $phoneNumber; //Outputs: 09999999999
A Note On Security
I strongly suggest that yo do not use $_REQUEST and instead use $_POST or $_GET as an extra security step. Explicitly using the method that you're expecting the data to come through is one more validation step to make sure that the request is legitimate.
elseif (!preg_match('/^[0-9]+$/', $_REQUEST['phone'] ) ) { should do the job. As the RegEx you use stands for the correct phone number, you need to issue the warning if there is no match found, not in case there is a match found.
Hope this will help
preg_match('/^\d{4}-\d{4}$/', $string);
^ Start of the string
\d{4} match a digit [0-9] Exactly 4 times
- matches the character - literally
\d{4} match a digit [0-9] Exactly 4 times
$ End of the string
$a = '2015-2016';
if(!preg_match('/^[0-9 \-]+$/',$a)) {
then return not valid data }
Try that.
There are three problems with your regex:
- You aren't escaping the forward slash. You're using the forward slash as a delimiter, so if you want to use it as a literal character inside the expression, you need to escape it
((.*?))doesn't do what you think it does. It creates two capturing groups -- one nested inside the other. I assume, you're trying to capture what's inside the parentheses. For that, you'll need to escape the(and)characters. The expression would become:\((.*?)\)- Your expression doesn't handle whitespace. In the string you've given, there is whitespace between the
</a>and the beginning of the number --</a> (2194). To ignore the whitespace and capture just the number, you need to use\s(which matches any whitespace character). For that, you need to write\s*\((.*?)\)\s*.
The final regular expression after fixing all the above errors, will look like:
$regex = '~Clasificación</a>\s*\((.*?)\)\s*</li>~';
Full code:
$string = 'Clasificación</a> (2194) </li>';
$regex = '~Clasificación</a>\s*\((.*?)\)\s*</li>~';
preg_match($regex , $string, $match);
var_dump($match);
Output:
array(2) {
[0]=>
string(32) "Clasificación (2194) "
[1]=>
string(4) "2194"
}
Demo.
You forget to espace / in your regex, since you're using the / as a delimiter:
$regex = '/Clasificación<\/a>((.*?))<\/li>/';
// ^ delimiter ^^ ^ delimiter
// ^^ / in a string which is escaped
Another way can be to change that delimiter, and then you will not have to escape it:
$regex = '#Clasificación<\/a>((.*?))<\/li>#';
See the PHP documentation for more information.
$string = '&filtered_features[48][]=491';
$string = preg_replace('/\[\d+\]\[\]=\d+/', '[][]=', $string);
echo $string;
I assume you wanted to remove the numbers from the string. This will match a multi-variable query string as well since it just looks for [A_NUMBER][]=A_NUMBER and changes it to [][]=
$query_string = "&filtered_features[48][]=491&filtered_features[49][]=492";
$lines = explode("&", $query_string);
$pattern = "/filtered_features\[([0-9]*)\]\[\]=([0-9]*)/";
foreach($lines as $line)
{
preg_match($pattern, $line, $m);
var_dump($m);
}

