Skip to content

Instantly share code, notes, and snippets.

@kostiantyn-petlia
Created March 15, 2019 10:17
Show Gist options
  • Save kostiantyn-petlia/53b185d3bf971a8dd11f765927027447 to your computer and use it in GitHub Desktop.
Save kostiantyn-petlia/53b185d3bf971a8dd11f765927027447 to your computer and use it in GitHub Desktop.
sanitize_numbers() & sanitize_absint()
/**
* Remove any chars except numbers
* '+1-012-123-45-678' => (string) '101212345678'
* 'user_15' => (string) '15'
*/
function sanitize_numbers( $string ) {
$string = is_numeric( $string ) ? (string) $string : $string;
return is_string( $string ) ? preg_replace( '/\D+/', '', $string ) : '';
}
/**
* Remove any chars except numbers
* 'term_15' => (int) 15
*/
function sanitize_absint( $string ) {
$id = sanitize_numbers( $string );
return ! empty( $id ) && is_numeric( $id ) ? (int) $id : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment