October 22, 2024
Chicago 12, Melborne City, USA
PHP

Regular expression to identify CSS functions


I have a variable called $value that contains a string with a css expression. It can be a value or a function:
rgb( 25, 128, 128 );, transform( -50% -50% );, bottom 50px right 100px;, #198080;, bold;, 1px solid red;

I want to be able to identify if the expression contains a function or a string

$real_value = (is_numeric($value)) ?
    $value :
    (is_string($value) ?
        (preg_match('/^\w/', $value) ? $value : '"' . $value . '"') :
        '"'. $value . '"');

The idea is to assign to $real_value the value inside quotation marks like this: "#198080"; or this "bottom 50px right 100px"; but if it is a function, just return the same expression.

I started creating my regular expression, but I can’t go past the first parentheses without breaking it: /^\w\-?\w?\(/

So I need to create a regular expression that checks if the string has the structure of a css function. There are not many of them (https://www.w3schools.com/cssref/css_functions.php) or just write one that checks for the parentheses



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video