Validate hex input close #151

This commit is contained in:
Brian Miyaji
2015-10-14 15:32:55 +11:00
parent dfb180d4da
commit d8d477c417

View File

@@ -268,12 +268,15 @@ if ( ! function_exists( 'sp_format_hex' ) ) {
*/ */
function sp_format_hex( $hex ) { function sp_format_hex( $hex ) {
$hex = preg_replace( '/[^A-Fa-f0-9]/', '', $hex );
$hex = trim( str_replace( '#', '', $hex ) ); $hex = trim( str_replace( '#', '', $hex ) );
if ( strlen( $hex ) == 3 ) { if ( strlen( $hex ) == 3 ) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
} }
$hex = substr( $hex, 0, 6 );
if ( $hex ) return '#' . $hex; if ( $hex ) return '#' . $hex;
} }
} }