我有一些情況。這個網站有兩部分是手機和主要網站。他們都從同一個數據庫/表中獲取內容。它是一個博客網站。當管理員使用文本編輯器(CKEditor)創建包含圖像的內容時,style
屬性將附加到生成的img
標記上。所以輸出看起來像這樣。PHP中失敗的正則表達式語法
<img alt="some content" src="some location" style="width:520px; height:600px;" />
這對主要網站非常有效,但在移動網站上圖像縮放比較差。 我有一個縮略圖腳本,可以解決這個問題,但我想要一種方式來獲得頁面加載前的src
屬性和刪除style
屬性的方法。
我使用正則表達式。
$str=$blog_post_column_from_database
$pattern=array ('#\<img alt="(.*?)" src="(.*)" style="(.*?)" /> #');
$replacement=array ('<img src="$my_thumbnailer_here.php?src=\\2" width="100%" />');
$a=(string)$str; //converts text to string to avoid code lines from executing
return preg_replace($pattern,$replacement,$a);
請問我做錯了什麼?..正則表達式不是我的強項謝謝。
正則表達式。改爲使用[DOM](http://php.net/dom)。 –
http://stackoverflow.com/questions/5517255/remove-style-attribute-from-html-tags –
@MarkResølved感謝您的鏈接..它效果很好,但不給我一個選項來放置我的thumnbail變量.. .Dont真的知道如何破解它... :) – jcobhams