Why did my preg_match() suddenly stop working? php

Rasmus Puls

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;
}
Wiktor Stribiżew

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

Expanded version:

^(
  \#[\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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP - Why is my preg_match not working?

From Dev

Why my preg_match() is not working?

From Dev

Why my preg_match() is not working?

From Dev

why did my .htaccess stop working on deployment

From Dev

why did my .htaccess stop working on deployment

From Dev

Why did my keyboard stop working?

From Dev

Why does my streamreader and writer suddenly stop working?

From Dev

Why did my UI Bootstrap datepicker-popup stop working?

From Dev

Java Why did my email session method stop working?

From Dev

PHP: preg_match not working

From Dev

Why did my username suddenly add @dhcp?

From Dev

laravel : php artisan suddenly stop working

From Dev

Simple php preg_match not working for me

From Dev

PHP mailing address preg_match() not working

From Dev

php preg_match() not working, find a var

From Dev

Why use of / in preg_match() function php

From Dev

Why did all my folder dates suddenly change?

From Dev

My pattern in preg_match not working as i want

From Dev

Cake php 1.3.14 suddenly stop save data while it was working before

From Dev

PHP preg_match not working as HTML5 pattern

From Dev

php preg_match s and m modifiers not working for multiple lines

From Dev

PHP preg_match not working as HTML5 pattern

From Dev

php preg_match not working as in other web applications

From Dev

Php preg_match optional slash not working properly

From Dev

Why did the D3 link labels stop working

From Dev

Why did NTFS-3G via Homebrew stop working?

From Dev

Why did ctrl-f stop working in LibreOffice?

From Dev

Why are backslashes used in preg_match function of PHP?

From Dev

Why are backslashes used in preg_match function of PHP?

Related Related

HotTag

Archive