1

PHPを使用してcssファイルから色を抽出しようとしています。これらの色は次のとおりです。

  1. 通常の色 (色: #xxxxxx;)
  2. 背景色 (背景: #xxxxxx; / 背景: ... #xxxxxx ...;)
  3. 背景のグラデーション (背景画像: 線形グラデーション (上、#xxxxxx、#xxxxxx);
  4. 色が 3 文字の場合もあります (例: #fff)

# で始まる単語を返すために preg マッチを試しました

preg_match_all('/(?!\b)(#\w+\b)/', $css_text, $matches)

...しかし、DIV ID (#header など) も返します

今後は、コードがカラー コードだけでなく、それが見つかった行番号も含む多次元配列を返すようにしたいと考えています。

助けてください!:)

---------- 編集: 解決した質問 ----------

回答ありがとうございます。皆さんの回答をまとめてみました。正規表現を最小限に抑えたかったので、これは最終的な作業コードとして使用したものです:

$css = file_get_contents("style.css");

$token = strtok($css, "{}");
$css_parts = array();
while ($token !== false) {
    $css_parts[] = trim($token);
    $token = strtok("{}");
}

$flag = false; $properties = "";
    foreach($css_parts as $part) {
    if($flag) { $properties .= " ".trim($part); }
    $flag = !$flag;
}
$properties = strtoupper(str_replace(array(":",",",";","(",")")," ",$properties));

$colors = array();
preg_match_all('/(?!\b)(#\w+\b)/',$properties,$colors);
$colors = array_unique($colors[0]);

print_r($colors);
4

3 に答える 3

1

3 つまたは 6 つの 16 進文字しか受け入れないため、この正規表現の方が正確であると思います。

/(?!\b)((#[0-9abcdef]{3}|#[0-9abcdef]{6})\b)/

ただし、これらの文字を含む ID も一致します。したがって、この問題を解決するために正規表現を使用することはお勧めしません。

于 2013-02-11T19:34:29.960 に答える
1

実際には、セレクター内に中括弧がないと想定している限り (通常は適切な想定です)、3 つのアプローチでこれをかなり簡単に行うことができます。

ステップ 1: {} 内のコンテンツを取得する

preg_match( '/\{([^\}]*)\}/gi' , $css_text , $lines );

ステップ 2: 色をつかむ

$colors = array();
$i = sizeof($lines);

while( $i-- ) {
    preg_match( '/(#[0-9a-f]{6}|#[0-9a-f]{3})/' , $line[$i] , $matches );
    $colors += $matches; //combine the arrays
}
于 2013-02-11T21:44:04.143 に答える
0

これは、正規表現を使用していない (行と文字をループする) いくつかのコードですが、これで作業が完了します。キーが 16 進数のカラー コードで、値がカラー キーが見つかった行番号の配列である連想配列を生成します。(rgba カラー キーも含めるように変更できます)。

$lst_lines = array(
   35 => "color: #000;text-decoration: none;",
   36 => "font-size: 2.5em;",
   37 => "margin-top: 5px;line-height: 60px;height: 60px;float: right;border-radius: 5px;box-  shadow: inset 0px 0px 0px 1px #872424, inset 0px 2px 0px 0px #F79494, inset 0px 0px 0px 2px #E78484;background: #A74444;background-image: linear-gradient(top, #A74444, #C76464);background-image: -moz-linear-gradient(top, #A74444, #C76464);background-image: -webkit-linear-gradient(top, #A74444, #C76464);background-image: -o-linear-gradient(top, #A74444, #C76464);background-image: -ms-linear-gradient(top, #A74444, #C76464);text-shadow: -1px -1px 0px rgba(0,0,0,0.5);",
   38 => "color: #1D1D1D;text-transform: uppercase;font-size: 1.1em;letter-spacing: -1px;text-decoration: none;color: #fff;opacity: 0.6;filter:alpha(opacity='50');",
);

// returns color in $str_line at position $int_pos
function findColor($int_pos, $str_line) {
    $str_temp = substr($str_line, $int_pos, 6);
    $lst_temp = str_split($str_temp);
    $lst_end = array(',', ';', ')', '}', ' ');
    $hex_color = '#';
    foreach ($lst_temp as $i => $c) {
        if (!in_array($c, $lst_end) && ($i < 6)) $hex_color .= $c;
        else break;
    }
    return $hex_color;
}

$arr_colors = array();    // This is the output array
foreach ($lst_lines as $int_no => $str_line) {
    $lst_chars = str_split($str_line);
    foreach ($lst_chars as $i => $c) {
        if ($c == '#') {
            $hex_col = findColor($i+1, $str_line);
            $arr_colors[$hex_col][] = $int_no;
        }
    }
}
于 2013-02-11T20:58:21.887 に答える