Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在数组中保存RGB(A)值?_Php_Arrays_Regex_Rgb_Rgba - Fatal编程技术网

Php 如何在数组中保存RGB(A)值?

Php 如何在数组中保存RGB(A)值?,php,arrays,regex,rgb,rgba,Php,Arrays,Regex,Rgb,Rgba,我正在尝试编写一个函数,该函数将采用RGB(a)颜色并将其值保存在一个数组中,并带有一个排除alpha值的选项。我很难弄清楚如何编写与输入匹配的正则表达式,但我认为这样的东西正是我想要的: function rgb_array( $color, $include_alpha = true ) { $pattern = 'what goes here?'; $color = preg_match( $pattern, $color, $matches); if ( $

我正在尝试编写一个函数,该函数将采用RGB(a)颜色并将其值保存在一个数组中,并带有一个排除alpha值的选项。我很难弄清楚如何编写与输入匹配的正则表达式,但我认为这样的东西正是我想要的:

function rgb_array( $color, $include_alpha = true ) {

    $pattern = 'what goes here?';

    $color = preg_match( $pattern, $color, $matches);

    if ( $include_alpha == true && ! empty( $matches[4] ) ) {
        $color = array( $matches[1], $matches[2], $matches[3], $matches[4] );
    } else {
        $color = array( $matches[1], $matches[2], $matches[3] );
    }

    return $color;
}
我希望能够向其提供任何有效形式的rgb/rgba:

rgb(0,0,0)
rgb(00,00,00)
rgb(0, 0, 0)
rgb(00, 00, 00)
rgba(0,0,0,0.5)
rgba(255, 255, 255, 1)
etc...
并让它生成一个数组:

[0] => '00', // red
[1] => '00', // green
[2] => '00', // blue
[3] => '1' // alpha only included if available and desired.
目前,我可以通过
str\u replace
实现这一点:

$color  = 'rgb(12, 14, 85)';
$remove = array( 'rgb', 'a', '(', ')', ' ' );
$color  = str_replace( $remove, '', $color );
$color  = explode( ',', $color );
但它感觉很粗糙,我找不到一个好方法来选择性地包含/排除alpha


感谢您的帮助,如果有一种完全不同于
preg\u match
的方法,那就更好了,我洗耳恭听。

稍微改进
explode
解决方案:

function rgb_array($color, $include_alpha = true)
{
    $color = trim($color, 'rgba()');       # "rgba(255, 255, 255, 1)" => "255, 255, 255, 1"
    $color = str_replace(' ', '', $color); # "255, 255, 255, 1"       => "255,255,255,1"
    $color = explode(',', $color);         # "255,255,255,1"          => ["255", "255", "255", "1"]
    $color[] = 1;                          # add extra value to be sure that all parsed colors have transparency

    return array_slice($color, 0, 3 + $include_alpha); # length is 3 or 4, depends on $include_alpha value
}
您还可以根据内置PHP类型强制转换为
float
int
从字符串中删除额外字符的事实,从字符串中提取数字:

function rgb_array($color, $include_alpha = true)
{
    $array = array_map('floatval', explode(',', $color));
    $array[] = 1;

    return array_slice($array, 0, 3 + $include_alpha);
}

如果您需要正则表达式解决方案,请参见:

rgba?\((\s?\d+),(\s?\d+),(\s?\d+)(?:,\s?(\d+(?:\.\d+)?))?\)
它首先匹配
'rgb'
,然后是可选的
'a'
和左
括号
,然后创建3个类似的组,匹配可选的白色
空格
,后跟一个或多个
数字
。第四组是可选的,将匹配一个或多个
数字
,可选地后跟一个
,以及一个或多个
数字

颜色将位于索引1、2、3和4(如果可用)。

您可以使用将字符串拆分为其组成部分。regex在一个前面的
rgba(
、a
或尾部
)处拆分字符串。
PREG\u SPLIT\u NO\u EMPTY
标志用于避免输出数组中出现空白值

$strings = array('rgb(0,0,0)',
'rgb(00,00,00)',
'rgb(0, 0, 0)',
'rgb(00, 00, 00)',
'rgba(0,0,0,0.5)',
'rgba(255, 255, 255, 1)');
foreach ($strings as $string) {
    print_r(preg_split('/rgba?\(\s*|\s*,\s*|\s*\)/', $string, -1, PREG_SPLIT_NO_EMPTY));
}
输出:

Array
(
    [0] => 0
    [1] => 0
    [2] => 0
)
Array
(
    [0] => 00
    [1] => 00
    [2] => 00
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
)
Array
(
    [0] => 00
    [1] => 00
    [2] => 00
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0.5
)
Array
(
    [0] => 255
    [1] => 255
    [2] => 255
    [3] => 1
)
Process: rgb(0,0,0)
array (
  0 => '0',
  1 => '0',
  2 => '0',
)
Process: rgb(00,00,00)
array (
  0 => '00',
  1 => '00',
  2 => '00',
)
Process: rgb(0, 0, 0)
array (
  0 => '0',
  1 => '0',
  2 => '0',
)
Process: donkey eggs
array (
)
Process: rgb(00, 00, 00)
array (
  0 => '00',
  1 => '00',
  2 => '00',
)
Process: rgba(0,0,0,0.5)
array (
  0 => '0',
  1 => '0',
  2 => '0',
  3 => '0.5',
)
Process: rgba(255, 255, 255, 1)
array (
  0 => '255',
  1 => '255',
  2 => '255',
  3 => '1',
)

我的答案不仅会提取您想要的子字符串值,而且还会执行相当高级别的验证(不是100%完美验证)。我从修改了模式,以便更准确地服务于这个问题。我相信你会发现这个答案是准确、优雅和直接的

如果您想剥离/简化模式,可以这样做:()
~^rgba?\(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d]+))?\)$~

代码:()()

输出:

Array
(
    [0] => 0
    [1] => 0
    [2] => 0
)
Array
(
    [0] => 00
    [1] => 00
    [2] => 00
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
)
Array
(
    [0] => 00
    [1] => 00
    [2] => 00
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0.5
)
Array
(
    [0] => 255
    [1] => 255
    [2] => 255
    [3] => 1
)
Process: rgb(0,0,0)
array (
  0 => '0',
  1 => '0',
  2 => '0',
)
Process: rgb(00,00,00)
array (
  0 => '00',
  1 => '00',
  2 => '00',
)
Process: rgb(0, 0, 0)
array (
  0 => '0',
  1 => '0',
  2 => '0',
)
Process: donkey eggs
array (
)
Process: rgb(00, 00, 00)
array (
  0 => '00',
  1 => '00',
  2 => '00',
)
Process: rgba(0,0,0,0.5)
array (
  0 => '0',
  1 => '0',
  2 => '0',
  3 => '0.5',
)
Process: rgba(255, 255, 255, 1)
array (
  0 => '255',
  1 => '255',
  2 => '255',
  3 => '1',
)

p、 如果你想使用
preg_split()
你可以对你的模式不太具体。这是给你的一个邮轮。()


为什么不简单地用逗号拆分,那么您将拥有一个包含每个逗号的数组。我目前正在使用
explode()
进行拆分,正如我在最后所示,但如果我保留该值,那么我需要一种方法从数组中排除alpha值,如果它不需要或传递的颜色不包含它。此值的可能重复项匹配无效的rgb内容,例如rbga(0,0,0)或rgb(0,0,0,0)。它也只允许数字周围有一个空格。稍微宽泛一点的方法是这样的(尽管这会根据类型将内容放在不同的位置参数中,并且还会遗漏许多其他有效的rgb/rgba内容,并且也会接受一堆无效的内容):/(?:(rgb)(\s*(\d+)\s*,\s*(\d+)\s*(\d+)\s*)(rgba)(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+),\s*(\d+),\s*)(\d+):\),\d+)。\d+):\\s*))/i再说一遍,这不是一个好的解决方案,所以不要使用它,但它显示了我的意思。谢谢!这是可行的,但我最终使用了
preg\u split
解决方案。另外,对于记录,我正在分别验证输入…第二个似乎很酷,但奇怪的是,它总是返回红色值为0…真的,我的坏。应该有额外的行
$color=trim($color,'rgba()')作为修复此问题的函数的第一行。谢谢!这似乎是最简单的,我最终使用
array\u pop()
有条件地删除alpha值。很好!这显然是最完整的答案(对不起,尼克,你的答案也很有效!)我并不真的需要验证,但这个函数比我验证的另一种方法运行得快得多,所以这仍然很有帮助。@shoeladed一点问题都没有-你应该始终接受最好的答案,即使在你接受另一个答案之后出现了。