简体   繁体   English

用PHP将内联样式转换为CSS

[英]convert inline styles to css with php

I have made a class that can produce allot of different barcode types in several formats like: - Any picture format, GIF, JPG, PNG, SVG,etc - Compressed javascript (JS) - Html with inline styles 我制作了一个类,可以产生几种格式的不同条形码类型的分配,例如:-任何图片格式,GIF,JPG,PNG,SVG等-压缩javascript(JS)-具有内联样式的HTML

The class is already optimized to reduce the output of barcode as small as is possible by 'drawing' only the information that is black. 该类已经过优化,可以通过仅“绘制”黑色信息来减少条形码的输出。

Because the HTML version of the barcode can be that huge (althought it is already optimized), i wonder if there is a class or function that can convert the inline styles to a style tag, for example: 因为条形码的HTML版本可能是如此之大(尽管已经进行了优化),所以我想知道是否存在可以将嵌入式样式转换为样式标签的类或函数,例如:

Output is (just a few lines): 输出为(仅几行):

<div id="mybarcode">
<div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"></div>
<div style="position:absolute;margin-left:34px;width:2px;height:128px;background:#000;"></div>
<div style="position:absolute;margin-left:36px;width:2px;height:48px;background:#000;"></div>
......
......

This is what i want: 这就是我要的:

<style>
     #mybarcode div {position:absolute;width:2px;height:128px;background:#000;}
    </style> 
    ....
    ....

    <div id="mybarcode">
    <div style="margin-left:26px;"></div>
    <div style="margin-left:34px;"></div>
    <div style="margin-left:36px;height:48px;">
    ....
    ....

If found a class that can do the opposite what i want but that is not my case. 如果找到一个可以做相反的事情,那不是我的情况。 Does anybody know if there is a class around that can do such things with html? 有人知道周围是否有可以使用html进行此类操作的类吗? It can also be useful converting other pages with inline styles to html with external css. 将具有内联样式的其他页面转换为具有外部CSS的html也很有用。

Thank you for your answer! 谢谢您的回答! kind regards. 亲切的问候。

Shouldn't be too hard. 不应该太难。

General outline of algorithm: 算法概述:

  • Look at each element and extract the inline style rules 查看每个元素并提取内联样式规则
  • Identify the styles that all elements have in common 识别所有元素共有的样式
  • Extract these styles into a style tag under a class (ie common ) 将这些样式提取到类(即common )下的style标签中
  • Remove the redundant inline styling and add the common class to the elements 删除多余的内联样式,并将common类添加到元素

You can do the actual element manipulation using Simple HTML Dom Parser . 您可以使用Simple HTML Dom Parser进行实际的元素操作。 The CSS rules themselves are pretty easy to parse, so Regexes would be what I'd use. CSS规则本身很容易解析,因此正则表达式将是我要使用的。 Especially in your case, since you wrote the code that generates the HTML so you'll know exactly what CSS rules you might encounter. 特别是在您的情况下,由于您编写了生成HTML的代码,因此您将确切知道可能遇到的CSS规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM