【问题标题】:Find color value into an array (string start by #)在数组中查找颜色值(以#开头的字符串)
【发布时间】:2013-12-14 09:01:45
【问题描述】:

我在 php 中编写了一个小函数,它从包含 css 中元素值的 css 文件创建一个数组。 现在我需要对这个数组进行循环并提取到另一个只包含颜色值的数组中(所有字符串都以#开头),但我不知道字符串的确切长度(有时是#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;
[...]

我需要扫描这个数组并创建一个只包含颜色值的新数组。

提前致谢

【问题讨论】:

    标签: javascript php html css arrays


    【解决方案1】:

    试试看

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

    【讨论】:

    • 谢谢,您的函数创建了一个包含所有颜色但也包含白色值的数组: Array ( [7] => Array ( ) [8] => Array ( ) [9] => Array ( ) [10] => 数组 ( [0] => #353535; [1] => 353535 ) [11] => 数组 ( [0] => #353535; [1] => 353535 )
    【解决方案2】:

    这样做:

    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
    )
    

    【讨论】:

    • 谢谢,我试过你的代码,数组包含颜色值,但有时也包含其他字符:[39] => #333; [40] => #333; [41] => #666), 到(#000)); [42] => #666, #000); [43] => #666666',endColors [44] => #000000'); [45] => #000; [46] => #444), to(#000)); [47] => #444, #000); [48] => #444444', endColors [49] => #000000'); [50] => #ffffff; [51] => #d7d7d7; [52] => #333;
    • 嗨@almal,已经更新了新的正则表达式。只想到;空格和行尾。认为新的正则表达式很好 - 如果您发现新的特性,请在 [^a|^b|..] 中添加新的分隔符。
    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多