我知道sanitize_hex_color
存在用于清理进入数据库的十六进制值(并且仅存在于自定义程序中),但转义这些值的最佳函数是什么。我应该使用sanitize_hex_color
? 是否有更好的执行功能?
RGBA值如何?
下面是我现在用来清理十六进制+rgba值的函数:
function example_sanitize_rgba( $color ) {
if ( \'\' === $color )
return \'\';
// If string does not start with \'rgba\', then treat as hex
// sanitize the hex color and finally convert hex to rgba
if ( false === strpos( $color, \'rgba\' ) ) {
return sanitize_hex_color( $color );
}
// By now we know the string is formatted as an rgba color so we need to further sanitize it.
$color = str_replace( \' \', \'\', $color );
sscanf( $color, \'rgba(%d,%d,%d,%f)\', $red, $green, $blue, $alpha );
return \'rgba(\'.$red.\',\'.$green.\',\'.$blue.\',\'.$alpha.\')\';
return \'\';
}
我可以用这个来转义相同的值吗?如果页面上有100多个值怎么办?看起来有点“沉重”。
非常感谢您的任何意见!
SO网友:ahmad araj
刚刚完成RGBA颜色的清理回调。并在我的主题和完美作品中进行了测试,并采用了RGBA价值观
请查找代码
function awstheme_sanitize_rgba( $color ) {
if ( empty( $color ) || is_array( $color ) )
return \'rgba(0,0,0,0)\';
// If string does not start with \'rgba\', then treat as hex
// sanitize the hex color and finally convert hex to rgba
if ( false === strpos( $color, \'rgba\' ) ) {
return sanitize_hex_color( $color );
}
// By now we know the string is formatted as an rgba color so we need to further sanitize it.
$color = str_replace( \' \', \'\', $color );
sscanf( $color, \'rgba(%d,%d,%d,%f)\', $red, $green, $blue, $alpha );
return \'rgba(\'.$red.\',\'.$green.\',\'.$blue.\',\'.$alpha.\')\';}