在 php 中使用正则表达式替换 SASS 中的颜色
Replace colors in SASS using regex in php
问题
我想在这里做的是打开一个 SASS 变量文件,然后在保留格式的同时替换冒号和分号之间的所有内容,其中在 [=25] 中用特定的选择器字符串作为前缀=],前置位需要根据 php 变量进行更改。我似乎确实能够计算出一些与 RegExr、/\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;
}
解决方案
经过更多搜索后,我遇到了 PHP Live Regex,这为我提供了一个更好的正则表达式计算工具,我将 str_replace
更改为 preg_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;
}
问题
我想在这里做的是打开一个 SASS 变量文件,然后在保留格式的同时替换冒号和分号之间的所有内容,其中在 [=25] 中用特定的选择器字符串作为前缀=],前置位需要根据 php 变量进行更改。我似乎确实能够计算出一些与 RegExr、/\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;
}
解决方案
经过更多搜索后,我遇到了 PHP Live Regex,这为我提供了一个更好的正则表达式计算工具,我将 str_replace
更改为 preg_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;
}