dsf22567 2013-08-27 09:31
浏览 36

将{}内的CSS拆分为键/值对数组

i need some help with a RegEx that should split cssText( not selector,but the part inside {} ) into assoc with key->value pairs array in PHP

Assuming the the selector part is removed and there is for instance this:

color: black;
font-family: \"Courier New\";
background: url(\"test.png\");
color: red;

Yes the string is escaped i did managed to do that when extracting the {} part.

BUT:

if background is dataURI or there is content prop set like those:

content:'1.test;2.blabla;';
background: white url('data:image/png;base64,iVBORw0KGgoAA
AANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABl...') no-repeat scroll left top;

the splitting RegEx i use:

preg_match_all('/([\w-]+)\s*:\s*([^;]*)\s*;?/i', $css, $matches, PREG_SET_ORDER);

fails :(

I'm not pro in RegEx and need your advice on how to NOT brake on ';' appearance inside \"...\" inside the string.

It should be in the capturing group ([^;]*) which as i found means: Match any single character that is not ; and repeat that 0 or more times.

Thanks in advance!

  • 写回答

4条回答 默认 最新

  • doubingguan3425 2013-08-27 09:46
    关注

    If you don't care about comments you could use something like this to handle quoted content with escapes:

    /([\w-]++) \s*+ : \s*+ ( (?: [^;'"]++ | "(?:[^"\\]|\\.)*+" | '(?:[^'\\]|\\.)*+' )*+ ) \s* ;?/x
    

    (Don't forget to double+ escape the \\ when quoting in PHP.)

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog