drvfqr5609 2012-03-26 05:37
浏览 30
已采纳

可以将样式表与内联样式合并的php库

I am working with html document generated from Micrsoft Word 2007/2010. Besides generating incredibly dirty html, word also has the tendency of using both block and inline style. I am looking for a php library would merge block into already existing inline style element.

Edit The goal is to construct a html block preserve the original formatting and editable in WYSIWYG editor like tinyMCE

Example

If the original html is:

    <html>
    <head>
    <style>
    .normaltext {color:black;font-weight:normal;font-size:10pt}
    .important {color:red;font-weight:bold;font-size:11pt}
    </style>
    <body>
    <p class="normaltext" style="font-family:arial">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
In ut erat id dui mollis faucibus. Mauris eu neque et eros tempus placerat. 
<span class="important">Nam in purus nisi</span>, vitae dictum ligula. 
Morbi mattis eros eget diam vulputate imperdiet. 
<span class="important" style="color:green">Integer</span> a metus eros. 
Sed iaculis porta imperdiet.
    </p>
    </body>
    </html>

Should become:

    <html>
    <head>
    <body>
    <p style="font-family:arial;color:black;font-weight:normal;font-size:10pt">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    In ut erat id dui mollis faucibus. Mauris eu neque et eros tempus placerat. 
    <span style="color:red;font-weight:bold;font-size:11pt">Nam in purus nisi</span>, vitae dictum ligula. 
    Morbi mattis eros eget diam vulputate imperdiet. 
    <span style="color:green;font-weight:bold;font-size:11pt">Integer</span> a metus eros. 
    Sed iaculis porta imperdiet.
    </p>
    </body>
    </html>
  • 写回答

4条回答 默认 最新

  • dt3999 2012-04-12 03:11
    关注

    I finally managed to get it to work. The code is based off of http://blog.verkoyen.eu/blog/p/detail/convert-css-to-inline-styles-with-php with once simple change: Moving the line

    // add new properties into the list
    foreach($rule['properties'] as $key => $value) $properties[$key] = $value;
    

    up to the begining of the loop, right after where $properties is declared.

    To make this work for WordPress however, one additional change is needed. DomDocument replace &nbps; from the document with blanks, which breaks WordPress update statement and lead to cotent being cut off. Please refer to my other question for the solution: DOMDocument->saveHTML() converting &nbsp; to space

    This problem is detailed in https://wordpress.stackexchange.com/questions/48692/post-content-getting-cut-off-on-blank-space-on-wpdb-update. If you know why this is happening for WordPress, please post your answer there as I would very much like to find out why it is happening.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线