2017-07-10 56 views
0

問題

什麼,我希望能夠在這裏做的就是打開一個SASS變量文件,然後同時保留格式取代它被換成了一個冒號和分號之間的一切PHP中的特定選擇器字符串,前置位需要根據php變量進行更改。我似乎能夠找出一些匹配RegExr,/\primary-color:(.*?)\;,/\primary-color:(.*?)\;上的字符串的正則表達式,但是當它嘗試在str_replace中使用它時,它沒有工作,添加到此我還需要用傳遞的php變量替換主要顏色選擇器進入正則表達式。更換顏色

文件內容

string(328) "// 1. Colors $primary-color: #00c853; $link-color: #ff9800; $secondary-color: #ef6c00; $success-color: green; $error-color: red; $info-color: blue; $warning-color: orange; // 6. Chips $chip-bg-color: #e4e4e4 !default; $chip-border-color: #9e9e9e !default; $chip-selected-color: #26a69a !default; $chip-margin: 5px !default; " 

PHP函數

/** 
* Set Sass Variables 
* 
* @param array $options contains the list of colors to use in the SASS 
*/ 
public function set_sass_color($option = array()){ 
    $option['str'] = 'Example Text'; 
    $option_name = 'primary-color'; 
    $search = "/\primary-color:(.*?)\;/"; 
    $variables = file_get_contents(get_template_directory().'/assets/styles/common/_variables.scss'); 
    $variables_after = str_replace($search,$option['str'],$variables); 
    return $variables_after; 
} 

回答

0

解決方案

後,一些搜索我碰到PHP Live Regex,這給了我一個更好的工具,工作了正則表達式,我改變str_replacepreg_replace,現在一切似乎按預期工作。

PHP函數

/** 
* Set Sass Variables 
* 
* @param array $option contains the option object for modifying the SASS 
*/ 
public function set_sass_color($option = array()){ 
    $option['str'] = 'here'; 
    $option_name = 'primary-color'; 
    $search = '/('.$option_name.')[:](.*?)[;]/'; 
    $variables = file_get_contents(get_template_directory().'/assets/styles/common/_variables.scss'); 
    $variables_after = preg_replace($search,$option['str'],$variables); 
    return $variables_after; 
}