php预匹配解析直方图字符串
我有字符串中的ImageMagick直方图颜色信息,我想使用php preg_match()函数解析字符串,对不起,我没有更多关于正则表达式的知识php预匹配解析直方图字符串,php,regex,imagemagick,preg-match,histogram,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); ?> 如何获取所需的输出,或者是否有其他方法获取颜色和密度?您
<?php
$str = "588: ( 99, 75, 52) #634B34 srgb(99,75,52)";
preg_match('/(?P<colors>\d+\:)/', $str, $matches);
print_r($matches);
?>
如何获取所需的输出,或者是否有其他方法获取颜色和密度?您可以使用此正则表达式获取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我想你想要这样的东西
(?P<colors>\d+):\s*\(\s*(?P<red>\d+),\s*(?P<green>\d+),\s*(?P<blue>\d+)
(?P\d+):\s*\(\s*(?P\d+)\s*(?P\d+)\s*(?P\d+)
从何处获取rgb值?您已经开始了正确的方法,只需继续匹配:…
\s*(\s*(?p\d+),\s*(?p\d+)
…您是说这个吗?@AvinashRaj是的,它符合要求谢谢,我们可以在颜色代码(\634B34
)之前确定目标值588:(99,75,52)
因为srgb(…)
not fixed可能是这里出现的颜色名称。第一个数字长度不固定,我尝试了您的代码没有输出您正在打印的内容(匹配项)不是($matches)!将其更改为$matches
(?P<colors>\d+):\s*\(\s*(?P<red>\d+),\s*(?P<green>\d+),\s*(?P<blue>\d+)