Home > Blockchain >  regex to isolate specific part of a in-line style from string
regex to isolate specific part of a in-line style from string

Time:10-21

I have the following string ($str) #FFFFFF;stop-opacity:0.6

One thing to consider is stop-opacity maybe buried in the string, the sample string above is just an example. Below are some alternate example positioning of the string:

stop-opacity:0.6;#FFFFFF;
#FFFFFF;stop-opacity:0.6;stop-color:#FFFFFF;

I am trying to extract stop-opacity and its value into a separate array (e.g. $arr) array("type"=>"stop-opacity","value"=>"0.6").

CodePudding user response:

I have created a sandbox so you can view and play with the code used for this answer.

The regex below is able to extract the string you need:

stop-opacity         # Find stop-opacity within the string
  \s*                # Match zero or more whitespace characters
     :               # Match the : character
      \s*            # Match zero or more whitespace characters
         (           # Start a capture group
          [0-9\.]    # Match characters 0-9 and . one or more times
                  )  # End the capture group

I have implemented the regex above into a simple PHP script below which shows its usage along with some testing, it also includes your requirements as stated in the question:

<?php

function getStopOpacity(string $line) {
    preg_match('/stop-opacity\s*:\s*([0-9\.] )/', $line, $matches);

    return [
        'type' => 'stop-opacity',
        'value' => $matches[1] ?? null
    ];
}

$tests = [
    "#FFFFFF;stop-opacity:0.6",
    "stop-opacity:0.6;#FFFFFF;",
    "#FFFFFF;stop-opacity:0.6;stop-color:#FFFFFF;",
    "#FFFFFF;stop-opacity  :      0.6  ;stop-color:#FFFFFF;"
];

foreach($tests as $test) {
    $result = getStopOpacity($test);

    echo $result['type'] . ": " . $result['value'] . "\n";
}

The above outputs:

stop-opacity: 0.6
stop-opacity: 0.6
stop-opacity: 0.6
stop-opacity: 0.6

I have updated the answer in response to the comment provided by OP.

The regex has been adjusted so that it can capture many different attribute values within a string:

stop-color          # Find stop-color within the string (but this can be changed to any attribute)
  \s*               # Match zero or more whitespace characters
     :              # Match the : character
      \s*           # Match zero or more whitespace characters
         (          # Start a capture group
          . ?       # Match any character (non-greedy)
             )      # End the capture group
              \s*   # Match zero or more whitespace characters
                 ;  # Match the ; character

There is a caveat with the regex above, the attribute value must have a ; character after it.

I have created a new sandbox so you can review the code.

The function has been updated to accept an attribute as a parameter, allowing you to extract many different key/value pairs from the string:

<?php

function getAttributeValue(string $attribute, string $line) {
    preg_match("/$attribute\s*:\s*(. ?)\s*;/", $line, $matches);

    return [
        'type' => $attribute,
        'value' => $matches[1] ?? null
    ];
}

$tests = [
    "#FFFFFF;stop-opacity:0.6;",
    "stop-opacity:0.6;#FFFFFF;",
    "#FFFFFF;stop-opacity:0.6;stop-color:#FFFFFF;",
    "#FFFFFF;stop-opacity  :      0.6  ;stop-color:red;"
];

foreach($tests as $test) {
    $result = getAttributeValue("stop-opacity", $test);

    echo $result['type'] . ": " . $result['value'] . "\n";
}

$tests = [
    "#FFFFFF;stop-opacity:0.6;stop-color:#FFFFFF;",
    "#FFFFFF;stop-opacity  :      0.6  ;stop-color:red;"
];

foreach($tests as $test) {
    $result = getAttributeValue("stop-color", $test);

    echo $result['type'] . ": " . $result['value'] . "\n";
}

The above outputs:

stop-opacity: 0.6
stop-opacity: 0.6
stop-opacity: 0.6
stop-opacity: 0.6
stop-color: #FFFFFF
stop-color: red
  •  Tags:  
  • php
  • Related