Regex for matching CSS hex colors

后端 未结 5 1150
傲寒
傲寒 2021-02-02 07:33

I\'m trying to write regex that extracts all hex colors from CSS code.

This is what I have now:

Code:

$css = <<

        
5条回答
  •  半阙折子戏
    2021-02-02 08:18

    The accepted answer shows you how to do it with regex, because that was your question. But you really don't need to use regex for this. Normally this is how I would do it:

    if(ctype_xdigit($color) && strlen($color)==6){
        // yay, it's a hex color!
    }
    

    for 100.000 iterations:

    Regex solution *: 0.0802619457245 seconds

    Xdigit with strlen: 0.0277080535889 seconds

    *: hex: ([a-fA-F0-9]{6})

提交回复
热议问题