我使用此函数来解析CSS文件,但是当涉及包含分号的属性值时,它会中断。例如:
#logo {
background-image: url("data:image/png;base64,..................");
}
我将爆炸分隔符更改为" \ n"但它没有产生预期的结果。 请帮忙。
功能:
function parse($file){
$css = file_get_contents($file);
preg_match_all( '/(?ims)([a-z0-9\s\.\:#_\-@,]+)\{([^\}]*)\}/', $css, $arr);
$result = array();
foreach ($arr[0] as $i => $x){
$selector = trim($arr[1][$i]);
$rules = explode(';', trim($arr[2][$i]));
$rules_arr = array();
foreach ($rules as $strRule){
if (!empty($strRule)){
$rule = explode(":", $strRule);
$rules_arr[trim($rule[0])] = trim($rule[1]);
}
}
$selectors = explode(',', trim($selector));
foreach ($selectors as $strSel){
$result[$strSel] = $rules_arr;
}
}
return $result;
}
执行:
$css = parse('content/custom.css');
echo print_r($css);
答案 0 :(得分:0)
如果为攻击性;base64
使用临时占位符字符串,然后执行拆分,然后将其还原,请执行以下操作:
替换此行:
$rules = explode(';', trim($arr[2][$i]));
用这个:
//replace ;base64 with a placeholder
$placeholder=uniqid();
$arr[2][$i] = str_replace(";base64",$placeholder,$arr[2][$i]);
//explode
$rules = explode(';', trim($arr[2][$i]));
//restore from placeholder
$rules = array_map(function ($el) {
return str_replace($placeholder, ";base64", $el);
}, $rules);