2010-11-23 59 views
0

鑑於該請求對象類的物品的串...正則表達式轉換Get請求對象CSS對象

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' 

什麼將正則表達式和PHP模樣採取的每個項目,並將其寫入到輸出的字符串,它可以保存爲custom.css命名的文件,使文件的最終內容是...

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

注:管在請求應該只是被轉化爲空的空間輸出。

回答

0

假設輸入數據是,如上所述,一個單獨的字符串與換行分隔的值,然後下面的preg_match_all和循環將工作創建一個字符串$ strCssFile,可以寫出一個CSS文件:

<?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

這看起來不錯。但是,在迴應我的選項值的過程中,似乎使用點「。」。在選項名稱foobars我用來發送選項到我的保存功能的數組。如果在期間「。」中標有星號「*」,代碼將會是什麼樣子? – 2010-11-23 22:50:00