duanlaican1849 2015-01-24 12:31
浏览 65
已采纳

php解析css类字符串

I need to get css class name, attributes and values in order to apply inline css style to a div. Currently, I can retrive css class name information from a string like this :

$class = ".custom_class {
    margin-top: 0px !important;
    border-top-width: 0px !important;
    border-right-width: 0px !important;
    border-left-width: 0px !important;
    padding-top: 250px !important;
    padding-bottom: 250px !important;
    background-image: url(http://localhost/wordpress/wp-content/uploads/2015/01/old-car.jpg?id=3663) !important;
    border-left-color: #dd3333 !important;
    border-right-color: #dd3333 !important;
    border-top-color: #dd3333 !important;
}";

How can I get or regex all css attributes and values in order to include them in a div like this:

<div class="custom_class" style="margin-top: 0px !important;border-top-width: 0px !important;border-right-width: 0px !important;border-left-width: 0px !important;padding-top: 250px !important;padding-bottom: 250px !important;background-image: url(http://localhost/wordpress/wp-content/uploads/2015/01/old-car.jpg?id=3663) !important;border-left-color: #dd3333 !important;border-right-color: #dd3333 !important;border-top-color: #dd3333 !important;"></div>

I tried with this: preg_match("/{(.*?)}/", $class, $match); But it outputs with brackets. And for the class name I don't know how to do that.

  • 写回答

1条回答 默认 最新

  • dtn36013 2015-01-24 12:54
    关注

    ou're near with your regex preg_match("/{(.*?)}/", $class, $match);, just escape the curly brackets and retrive the data in $match[1]:

    preg_match("/^([\w.]+)\s*\{(.*?)\}/s", $class, $match);
    $classname = $match[1];
    $style = $match[2];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题