Tutorial :Regex help, greedy vs. non-greedy



Question:

Hey all I have a large html string like

<a style="background: rgb(100, 101, 43) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-backg    round-inline-policy: -moz-initial;" href="#">swatch4</a>    <a style="background: rgb(34, 68, 33) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-b    ackground-inline-policy: -moz-initial;" href="#">swatch5</a>    <a style="background: rgb(11, 38, 68) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -m    oz-background-inline-policy: -moz-initial;" href="#">swatch6</a>    <a style="background: rgb(39, 11, 60) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial    ; -moz-background-inline-policy: -moz-initial;" href="#">swatch7</a>  

...There are lot of these entries...

What I would like to end up with is a php array

$colors = array(   'rgb(34, 34, 33)',   'rgb(11, 38, 68)',  ...  );   

etc.

The step I need help with extracting the rgb part of the string. I am having trouble with my regexs eating too much of the string. Anyhelp would be appreciated, i would like to get this down and learn how to do (as opposed to just having it done). I am using vim btw.

Thanks


Solution:1

The simple regex is

(rgb\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\))  

Each "\s*\d{1,3}\s*," means:

  • match space (space like ' ', or tabs, as molf says in the comments, in a number from 0 to inf)
  • match a digit (from 1 digit up to 3 digits because you go from 0 up to 255)
  • match other spaces
  • match a comma (,)

You can use Paolo's code for the array part.


Solution:2

preg_match_all('/rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)/', $string, $matches);  print_r($matches);  

If you're not sure about the spacing, use:

preg_match_all('/rgb\(\d{1,3},\s?\d{1,3},\s?\d{1,3}\)/', $string, $matches);  print_r($matches);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »