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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?