转义十六进制/RGBA值

时间:2017-02-22 作者:Tom Usborne

我知道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多个值怎么办?看起来有点“沉重”。

非常感谢您的任何意见!

1 个回复
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.\')\';}

相关推荐

Disable escaping html

我在用SyntaxHighlighter Evolved 突出显示代码示例。E、 g。[csharp] string s = \"text\"; List<int> numbers = new List<int>(); [/csharp] 当我第一次保存它时,没关系,但编辑wordpress时,文本会更改为[csharp] string s = &quot;text&quot;; List&lt;int&am