Hi I have had problems with these preg_match before. I know it has something to do with the "delimeter" but I don't know why and how to fix...
This function was perfectly working for a week. But suddenly today it stopped matching. I thought that it had something to do with some other code that I had added during the day. So to localise the problem I rolled back my files to a previous version where it was working. Only to realise that it is no longer working..............
Is it something with the memory in my server that caches some of the pattern? Or how can it be that something that have been working isn't working anymore?
This is my function, it was beautifully returning color code in all allowed formats, but now it is ONLY returning #000000 no matter what I feed it.
function validateColor($input){
$match = preg_match("/^(\#[\da-f]{3}|\#[\da-f]{6}|rgba\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)(,\s*(0\.\d+|1))\)|hsla\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)(,\s*(0\.\d+|1))\)|rgb\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)|hsl\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)\))$/",$input);
if(!$match){
return '#000000';
}
return $input;
}
You need to make sure you match a )
in rgb
alternative, add \)
there:
/^(\#[\da-f]{3}|\#[\da-f]{6}|rgba\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)(,\s*(0\.\d+|1))\)|hsla\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)(,\s*(0\.\d+|1))\)|rgb\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)\)|hsl\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)\))$/
See the regex demo
^(
\#[\da-f]{3}
|\#[\da-f]{6}
|rgba\(
((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}
((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)
(,\s*(0\.\d+|1))
\)
|hsla\(
\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,
\s*((\d{1,2}|100)\s*%)\s*,
\s*((\d{1,2}|100)\s*%)
(,\s*(0\.\d+|1))
\)
|rgb\(
((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}
((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)
\) # HERE
|hsl\(
\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,
\s*((\d{1,2}|100)\s*%)\s*,
\s*((\d{1,2}|100)\s*%)
\)
)$
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments