2012-03-26 69 views
0

我正在處理從Micrsoft Word 2007/2010生成的html文檔。除了生成令人難以置信的髒html外,word還具有使用塊和內聯風格的趨勢。我正在尋找一個PHP庫將合併塊成已經存在的內聯樣式元素。php庫,可以合併樣式表與內聯樣式

編輯 的目標是建立一個HTML塊保存在所見即所得的編輯器,如tinyMCE的

原來的格式和編輯如果原始的HTML是:

<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> 

應該變成:

<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> 
+0

所以你試圖擺脫CSS塊,並將其全部內聯?或者相反呢? – 2012-03-26 05:39:33

+3

這個值是值得懷疑的。這是一個複雜的任務,因爲它需要完整的DOM和CSS解析器,並且可能導致比現有更多的擴展。 – SpliFF 2012-03-26 05:43:46

+0

從CSS塊內聯。 – ltfishie 2012-03-26 15:00:12

回答

0

我終於設法讓它工作。該代碼是基於關閉的 http://blog.verkoyen.eu/blog/p/detail/convert-css-to-inline-styles-with-php 用一次簡單的變化: 移動線

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

到循環的開頭,就在$性能聲明之後。

爲了使這項工作爲WordPress,但需要一個額外的變化。 DomDocument用空白替換&nbps;,這會破壞WordPress更新聲明並導致強制中斷。請參閱我的其他問題的解決方案: DOMDocument->saveHTML() converting &nbsp; to space

此問題詳細https://wordpress.stackexchange.com/questions/48692/post-content-getting-cut-off-on-blank-space-on-wpdb-update。如果你知道爲什麼WordPress會發生這種情況,請在那裏發佈你的答案,因爲我非常想知道它爲什麼會發生。

0

不,但試試這個,而從字複製和粘貼到http://ckeditor.com/或tinymce等清理它很多,認爲它還不完美,它會讓你更接近。

1
+0

謝謝,emogrifier看起來非常接近。將嘗試玩它。已經嘗試http://blog.verkoyen.eu/blog/p/detail/convert-css-to-inline-styles-with-php並且不合並樣式。 – ltfishie 2012-04-02 01:29:48