我在php中编写了一个小函数,它从css文件中创建一个包含css中元素值的数组。 现在我需要cicle这个数组并提取另一个只包含颜色值的数组(所有字符串以#开头)但我不知道确切的字符串长度(有时#FFF,其他时间#353535或#00 ecc)< / p>
我的数组的一个例子是:
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;
[...]
我需要扫描这个数组并创建一个只包含颜色值的新数组。
提前致谢
答案 0 :(得分:2)
尝试
foreach($my_arr as $str) {
preg_match('/'.preg_quote('#').'(.*?)'.preg_quote(';').'/', $str, $match);
if($match) {
$res_arr[] = $match;
}
}
print_r($res_arr);
答案 1 :(得分: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
)