开发者

Regex to convert Get request objects to CSS objects

Given a string of items in the request object like...

mytheme_color|body = '000000'
mytheme_color|h1 = 'ffffff'
mytheme_color.footer|a = 'cccccc'
mytheme_color.menu.top|a = 'ff0000'
mytheme_color.content|ul|li#test = '777777'

What would 开发者_开发知识库the regex and PHP look like to take each item and write them to an output string that could then be saved as a file named custom.css, so that the final contents of the file is...

body {color:#000000;}
h1 {color:#ffffff;}
.footer a {color:#cccccc;}
.menu.top a {color:#ff0000;}
.content ul li#test {color:#777777;}

Note: pipe in request should just be converted to empty space for output.


Assuming that the input data is, as described above, a single string with the values seperated by line breaks then the following preg_match_all and loop will work to create a string called $strCssFile which can be written out to a CSS file:

<?php

// Test dummy data
$strTest = "mytheme_color|body = '000000'
mytheme_color|h1 = 'ffffff'
mytheme_color.footer|a = 'cccccc'
mytheme_color.menu.top|a = 'ff0000'
mytheme_color.content|ul|li#test = '777777'";

$arrOutput = array();
preg_match_all("/mytheme_color([a-z0-9#\.|]+) = '([a-f0-9]+)'/i", $strTest, $arrOutput);
//preg_match_all("/mytheme_color([a-z0-9#\*|]+) = '([a-f0-9]+)'/i", $strTest, $arrOutput);

$strCssLine = '';
$strCssFile = '';

foreach ($arrOutput[1] as $intKey => $strValue) {
    $strCssLine = trim(str_replace('|', ' ', $strValue));
    //$strCssLine = trim(str_replace(array('|', '*'), array(' ', '.'), $strValue));
    $strCssLine .= ' { color: ';
    $strCssLine .= $arrOutput[2][$intKey];
    $strCssLine .= "; }\n";
    $strCssFile .= $strCssLine;
}

echo $strCssFile;

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜