我在字符串中有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
)
如何获得所需的输出,还是有其他方法可以获得颜色和密度?
答案 0 :(得分:1)
答案 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