Skip to content

Instantly share code, notes, and snippets.

@nagiyevelchin
Last active February 12, 2022 15:52
Show Gist options
  • Save nagiyevelchin/bb6b4fc5ba125c1bc214b41cb823f406 to your computer and use it in GitHub Desktop.
Save nagiyevelchin/bb6b4fc5ba125c1bc214b41cb823f406 to your computer and use it in GitHub Desktop.
Finds whether a variable is a needed type in PHP
<?php
/**
* Finds whether a variable is a unsigned float or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isUnsignedFloat($val) {
$val = str_replace(" ", "", trim($val));
return preg_match("/^([0-9])+([\.|,]([0-9])*)?$/i", $val);
}
/**
* Finds whether a variable is a unsigned integer or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isUnsignedInteger($val) {
$val = str_replace(" ", "", trim($val));
return preg_match("/^([0-9])+$/i", $val);
}
/**
* Finds whether a variable is a signed float or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isSignedFloat($val) {
$val = str_replace(" ", "", trim($val));
return preg_match("/^-?([0-9])+([\.|,]([0-9])*)?$/i", $val);
}
/**
* Finds whether a variable is a signed integer or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isSignedInteger($val) {
$val = str_replace(" ", "", trim($val));
return preg_match("/^-?([0-9])+$/i", $val);
}
/**
* Finds whether a variable is a float or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isFloat($val) {
$val = str_replace(" ", "", trim($val));
return preg_match("/^([0-9])+([\.|,]([0-9])*)?$/i", $val) || preg_match("/^-?([0-9])+([\.|,]([0-9])*)?$/i",
$val);
}
/**
* Finds whether a variable is a integer or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isInteger($val) {
$val = str_replace(" ", "", trim($val));
return preg_match("/^([0-9])+$/i", $val) || preg_match("/^-?([0-9])+$/i", $val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment