I have ImageMagick histogram color info in string, I want to parse string using php preg_match() function, sorry I have not more knowledge about Regular Expression.
<?php
$str = "588: ( 99, 75, 52) #634B34 srgb(99,75,52)";
preg_match('/(?P<colors>\d+\:)/', $str, $matches);
print_r($matches);
?>
Expected output
Array
(
[colors] => 588,
[red] => 99,
[green] => 75,
[blue] => 52
)
How do I get the desired output, or is there another way to get colors and density?
I think you want something like this,
(?P<colors>\d+):\s*\(\s*(?P<red>\d+),\s*(?P<green>\d+),\s*(?P<blue>\d+)
you can use this regex for getting the values in srgb:
$str = "588: ( 99, 75, 52) #634B34 srgb(99,75,52)";
preg_match_all("/srgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/i", $str, $matches);
var_dump($matches);
so if you have your string always like this:
substr($str, 0,3);
will be 588