i have write a little function in php that create an array from a css file contained values of element in css. Now i need to cicle this array and extract in another array containing just the value of colors (all string start by #) but i don't know exact lenght of string (sometime #FFF, other time #353535 or #00 ecc)
An example of my array is:
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;
[...]
I need to scan this array and create a new one contain just colors value.
Thanks in advance
Try like
foreach($my_arr as $str) {
preg_match('/'.preg_quote('#').'(.*?)'.preg_quote(';').'/', $str, $match);
if($match) {
$res_arr[] = $match;
}
}
print_r($res_arr);
Do this :
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;
}
}
the regex takes all occurences of something starting with #
and ending with ;
(or end of line)
the $hexColors
array will now contain :
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
)