리자님께 문득 궁금해서 하나 남깁니다. > 정규표현식

정규표현식

리자님께 문득 궁금해서 하나 남깁니다. 정보

리자님께 문득 궁금해서 하나 남깁니다.

본문

리자님이, 일전에 다크모드 작업하실때

정규표현식으로 무언가 하셧다고 하는데, 그게 몬지 알려주실수 있을까요?

 

css 칼라코드 일괄 처리.. 이었던걸로 어렴풋이 기억합니다만.. ^^

 

 

추천
1
  • 복사

댓글 4개

나중에 정리할 시간이 없겠네요. 그냥 올립니다. 뭐라고 하지 마세요~ ㅠㅠ

css 디렉토리에 php 파일 만드셔서 실행하시면 됩니다.

default.css 를 default.dark.css 로 만들어줍니다.


<?php
// 이렇게 바꿔주는 함수가 있을것 같은데 못찾겠다 ㅠㅠ
function reverse_color($c) {
    $colors = array(
        '0'=>'f',
        '1'=>'e',
        '2'=>'d',
        '3'=>'c',
        '4'=>'b',
        '5'=>'a',
        '6'=>'9',
        '7'=>'8',
        '8'=>'7',
        '9'=>'6',
        'a'=>'5',
        'b'=>'4',
        'c'=>'3',
        'd'=>'2',
        'e'=>'1',
        'f'=>'0');
    return $colors[$c];
}

function convert_color($str) {
    $colors = array(
        '000000'=>'ffffff',
        '111111'=>'eeeeee',
        '222222'=>'dddddd',
        'ffffff'=>'000000',
    );
    if ($colors[$str])
        return $colors[$str];
    else {
        print("<div><span style='color:red; font:15px 돋움체;'>#$str</span> 치환 코드 없음</div>\n");
        return $str;
    }
}

function convert_rgba($str) {
    $colors = array(
        'rgba(64,112,253,1)'=>'rgba(255, 255, 255, 0.15)',
        'rgba(255, 255, 255, 0.15)'=>'rgba(64,112,253,1)',
    );
    if ($colors[$str])
        return $colors[$str];
    else {
        print("<div><span style='color:blue; font:15px 돋움체;'>$str</span> 치환 코드 없음</div>\n");
        return $str;
    }
}

function css_dark_converter($source_css_file) {
    $target_css_file = preg_replace("|(.*)(\.css)$|", "$1.dark$2", $source_css_file);

    if (file_exists($target_css_file)) unlink($target_css_file);

    print("<h3><font color='blue'>{$source_css_file}</font> 파일을 <font color='red'>{$target_css_file}</font> 파일로 다크모드 생성중</h3>\n");
    //die($target_css_file);
    $source_file = fopen($source_css_file, 'r');
    $target_file = fopen($target_css_file, 'w');
    while (($line = fgets($source_file)) !== false) {
        //if (preg_match("|color\s*:|i", $line) || preg_match("|\#[0-9a-f]{3,6}|i", $line)) {
        if (preg_match("|\#[0-9a-f]{3,6}|i", $line)) {
            $line = preg_replace_callback("|\#([0-9a-f]{3,6})|i", function ($color_matches) {
                $color = $color_matches[1];
                $dark_color = '';
                for($i=0; $i<strlen($color); $i++) {
                    $dark_color .= reverse_color(strtolower($color[$i]));
                }
                return '#'.$dark_color;
            }, $line);
        }
        else if (preg_match("|(rgba\(([^\)]+)\))|", $line)) {
            $line = preg_replace_callback("|(rgba\(([^\)]+)\))|", function ($rgba_matches) {
                $rgba = $rgba_matches[1];
                return convert_rgba($rgba);
            }, $line);
            //print($line);
        }

        fwrite($target_file, $line);
    }

    fclose($source_file);
    fclose($target_file);
    print("<h3>{$target_css_file} 생성 완료</h3>\n");
}

css_dark_converter("header.css");
css_dark_converter("default.css");
css_dark_converter("mobile.css");
css_dark_converter("default_cmall.css");
css_dark_converter("mobile_cmall.css");
css_dark_converter("manual.css");
css_dark_converter("select.css");
?>
© SIRSOFT
현재 페이지 제일 처음으로