Skip to content

Instantly share code, notes, and snippets.

@borkweb
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save borkweb/9ac8ad13d86cdbb42844 to your computer and use it in GitHub Desktop.

Select an option

Save borkweb/9ac8ad13d86cdbb42844 to your computer and use it in GitHub Desktop.
<?php
function absint( $maybeint ) {
return abs( intval( $maybeint ) );
}
function sanitize_absint( $number = null ){
// If it's not numeric we forget about it
if ( ! is_numeric( $number ) ){
return false;
}
// Split the String into multiple pieces (handles 32bits)
$pieces = str_split( (string) $number );
// Make sure every instance is an absolute integer no negatives
$pieces = array_map( 'absint', $pieces );
// Remove any items that are not numeric
$pieces = array_filter( $pieces, 'is_numeric' );
// If after all it's empty just return false
if ( empty( $pieces ) ){
return false;
}
// Glue everything and ship it
return implode( '', $pieces );
}
function sanitize_regex( $val ) {
// If it's not numeric we forget about it
if ( ! is_numeric( $val ) ){
return false;
}
$val = preg_replace( '/[^0-9]/', '', $val );
if ( empty( $val ) ) {
return false;
}
return $val;
}
define( 'NUM', 1000000 );
$val = '9437987349173409109378412094789027410987409127842930874902137414';
$start = microtime( true );
for ( $i = 0; $i < NUM; $i++ ) {
sanitize_absint( $val );
}
$end = microtime( true );
echo 'Sanitize absint', $end - $start, ' seconds', "\n";
$start = microtime( true );
for ( $i = 0; $i < NUM; $i++ ) {
sanitize_regex( $val );
}
$end = microtime( true );
echo 'Sanitize regex', $end - $start, ' seconds', "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment