Created
March 15, 2019 10:17
-
-
Save kostiantyn-petlia/53b185d3bf971a8dd11f765927027447 to your computer and use it in GitHub Desktop.
sanitize_numbers() & sanitize_absint()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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