php preg_match解析直方图字符串

时间:2014-10-13 11:10:40

标签: php regex imagemagick preg-match histogram

我在字符串中有ImageMagick直方图颜色信息,我想用php preg_match()函数解析字符串,抱歉我对正则表达式没有更多的了解。

<?php 
 $str = "588: ( 99, 75, 52) #634B34 srgb(99,75,52)";
 preg_match('/(?P<colors>\d+\:)/', $str, $matches);
 print_r($matches);
?>

预期输出

Array
(
    [colors] => 588,
    [red] => 99,
    [green] => 75,
    [blue] => 52
)

如何获得所需的输出,还是有其他方法可以获得颜色和密度?

2 个答案:

答案 0 :(得分:1)

我想你想要这样的东西,

(?P<colors>\d+):\s*\(\s*(?P<red>\d+),\s*(?P<green>\d+),\s*(?P<blue>\d+)

DEMO

答案 1 :(得分:0)

您可以使用此正则表达式获取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);

所以如果你的字符串总是这样:

substr($str, 0,3);

将是588