2013-11-28 20 views
2

我已经在php中编写了一个小函数,该函数从css中包含元素值的css文件创建数组。 现在我需要cicle该数组,并提取另一个数组中包含颜色的值(所有字符串由#开始),但我不知道字符串的确切长度(有时#FFF,其他时间#353535或#00 ecc)在一个数组中查找颜色值(字符串以#开始)

我阵列的一个例子是:

Array 
(
[0] => margin: 0;padding: 0;border: 0;outline: 0;font-weight: inherit;font-style: inherit;font-size: 100%;font-family: inherit;vertical-align: baseline; 
[1] => list-style: none; 
[2] => border:none 
[3] => display: block 
[4] => width:100%;height:100%; 
[5] => font-family: Arial, Helvetica, sans-serif;font-size: 13px;color: black;margin:0 0 1px;line-height: 1.5;background-image:url(../images/bg1.png);background-position:left top;background-repeat:repeat; 
[6] => margin-bottom:15px; 
[7] => text-decoration:none; 
[8] => text-decoration:underline; 
[9] => font-family:Georgia, "Times New Roman", Times, serif;font-weight: normal;position:relative; 
[10] => font-size: 35px;line-height:1.6;color:#353535;text-transform:capitalize;text-align:left;margin-left:40px;border-bottom:1px dotted #353535; 
[11] => line-height:1.7px;color:#353535;font-size:14px;text-transform:none;display:block; 
[12] => font-size: 18px;line-height:1.7;color:#353535;text-align:left;width:350px;padding-top:8px;margin-left:40px; 
[13] => font-size: 28px;line-height:1.6;color:#353535;text-transform:none;text-align:left;background-color:transparent;padding-top:12px;margin-bottom:9px;border-bottom:1px dotted #353535; 
[14] => font-size: 12px;color: #353535;text-transform:capitalize;height:24px;margin-top:15px;text-align:left;display:block; 
[15] => font-size: 18px;line-height:1.7;color:#353535;text-align:left;width:350px;padding-top:8px;margin-bottom:12px; 
[16] => font-weight:bold;font-size:15px;font-family:Georgia, "Times New Roman", Times, serif;background-color:#FFCC33;padding:8px;margin-right:20px;-webkit-border-radius: .5em;-moz-border-radius: .5em;border-radius: .5em; 
[17] => font-size: 18px;line-height:1.3;color:#353535;text-align:left;padding-top:8px;margin-bottom:17px; 
[18] => font-size: 14px;font-weight:bold;text-align:left;margin-top:0;padding-top:0;padding-bottom:3px;margin-bottom:0; 
[19] => font-size: 28px;line-height:1.6;color:#353535;text-transform:none;text-align:left;background-color:transparent;padding-top:12px;margin-bottom:15px; 
[20] => border:none;border-top:1px dotted #999; 
[21] => padding:5px;border:solid 1px #cccccc;margin-bottom:10px; 
[22] => font-weight:bold;font-family:Georgia, "Times New Roman", Times, serif;width:980px;text-align:right;padding-right:40px;color:#333;margin:auto;margin-top:5px;margin-bottom:3px; 
[23] => font-weight:bold;font-family:Georgia, "Times New Roman", Times, serif;text-decoration:none;color:#333; 
[24] => text-decoration:underline; 
[25] => position: relative;width:100%; 
[26] => position:relative;width:980px;margin:0 auto;text-align: justify;background-color:white;padding:15px;overflow: hidden;-moz-box-shadow: 0 0 5px 5px #D8D8D8;-webkit-box-shadow: 0 0 5px 5px #D8D8D8;box-shadow: 0 0 5px 5px #D8D8D8; 
[27] => float: left;width: 613px;position: relative;background:white;padding:15px;margin-bottom:10px; 
[28] => color:#000 
[29] => color: #FC0;text-decoration:none; 
[...] 

我需要扫描该阵列,并创建一个新的只包含颜色值。

在此先感谢

回答

2

尝试像

foreach($my_arr as $str) { 
    preg_match('/'.preg_quote('#').'(.*?)'.preg_quote(';').'/', $str, $match); 
    if($match) { 
     $res_arr[] = $match; 
    } 
} 
print_r($res_arr); 
+0

感谢您的函数创建一个数组与所有的颜色,但也与白值:阵列 ( [7] =>数组 ( ) [8] =>阵列 ( ) [9] =>数组 ( ) [10] =>数组 ( [0] =>#353535; [1] => 353535 ) [11] =>数组 ( [0] =>#353535; [1] => 353535 ) – AleMal

1

这样做:

foreach ($array as $css) { 
    //preg_match_all('/\#(.+?)(;|s|$)/', $css, $colors); 
    preg_match_all('/\#([A-Fa-f0-9]*)([^;|^\)|^\'|^s|$])/', $css, $colors); 
    foreach ($colors[0] as $color) { 
     if ($color!='') $hexColors[]=$color; 
    } 
} 

正则表达式需要的东西所有出现开始#;结束(或行尾)

$hexColors阵列现在将包含:

Array 
(
    [0] => #353535 
    [1] => #353535 
    [2] => #353535 
    [3] => #353535 
    [4] => #353535 
    [5] => #353535 
    [6] => #353535 
    [7] => #353535 
    [8] => #FFCC33 
    [9] => #353535 
    [10] => #353535 
    [11] => #999 
    [12] => #cccccc 
    [13] => #333 
    [14] => #333 
    [15] => #D8D8D8 
    [16] => #D8D8D8 
    [17] => #D8D8D8 
    [18] => #000 
) 
+0

谢谢,我试图您的代码,该阵列含有的色彩价值,但有时也像这样的其他字符:[39] =>#333; [40] =>#333; [41] =>#666)至(#000)); [42] =>#666,#000); [43] =>#666666',endColors [44] =>#000000'); [45] =>#000; [46] =>#444)至(#000)); [47] =>#444,#000); [48] =>#444444',endColors [49] =>#000000'); [50] => #ffffff; [51] =>#d7d7d7; [52] =>#333; – AleMal

+0

hi @almal,已经更新了一个新的正则表达式。只想到;空间和行结束。认为新的正则表达式是好的 - 如果你发现新的特性,在'[^ a |^b | ..]'里面添加新的分隔符。 – davidkonrad

相关问题