function sanitize_key( $string, $replacement = '_' )
{
// Lowercase and remove accents
$string = htmlentities( trim( strtolower( strval( $string ) ) ), ENT_NOQUOTES );
$string = preg_replace( '/&([a-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);/', '$1', $string );
$string = preg_replace( '/&([a-z]{2})(?:lig);/', '$1', $string );
$string = preg_replace( '/&[^;]+;/', $replacement, $string );
// Replace non-alphanumeric characters
return preg_replace( '/[^a-z0-9]+/', $replacement, $string );
}
Sanitize $value According to its $type
function sanitize( $value, $type = null )
{
// Recursive sanitation for arrays and objects
if ( is_array( $value ) || is_object( $value ) ) {
$output = array();
foreach ( (array) $value as $k => $v ) {
$k = sanitize_key( $k );
$output[$k] = sanitize( $v, $type );
}
return $output;
}
// Default to value's actual type
if ( ! $type ) { $type = gettype( $value ); }
switch( strtolower( $type ) )
{
case 'boolean':
return boolval( $value );
case 'integer':
case 'number':
return intval( $value );
case 'double':
case 'float':
return floatval( $value );
case 'null':
return null;
case 'resource':
return $value;
case 'url':
return filter_var( rtrim( $value, '/' ), FILTER_SANITIZE_URL );
case 'email':
return filter_var( substr( $value, 0, 254 ), FILTER_SANITIZE_EMAIL );
default:
// String
return filter_var( $value, FILTER_SANITIZE_STRING );
}
}
Is the Request Made with Ajax ?
function is_ajax_request() {
return ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] )
&& strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) === 'xmlhttprequest';
}
Is the Browser Older Than Internet Explorer $version ?
function is_lt_ie( $version = 8 )
{
preg_match( '/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches );
if ( ! isset( $matches[1] ) ) {
preg_match( '/Trident/d{1,2}.d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches );
}
return ( isset( $matches[1] ) && $matches[1] < $version );
}