2017-07-10 53 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; 
}