PHP正則刪除HTML代碼中寬高樣式的方法
本文實例講述了PHP正則刪除HTML代碼中寬高樣式的方法。分享給大家供大家參考,具體如下:
因工作需要,需要采集html,并把html內(nèi)容保存到數(shù)據(jù)庫中。為了避免影響使用,寬高樣式需要刪除。例如圖片和div中的width, height等。
不過采集到的html中,樣式的寫法各有不同,例如大小寫,中間有空格等。
因此使用php正則編寫了下面這個方法,對這些奇葩的樣式進(jìn)行過濾。
代碼如下:
<?php
/**
* 清除寬高樣式
* @param String $content 內(nèi)容
* @return String
*/
function clear_wh($content){
$config = array('width', 'height');
foreach($config as $v){
$content = preg_replace('/'.$v.'\s*=\s*\d+\s*/i', '', $content);
$content = preg_replace('/'.$v.'\s*=\s*.+?["\']/i', '', $content);
$content = preg_replace('/'.$v.'\s*:\s*\d+\s*px\s*;?/i', '', $content);
}
return $content;
}
?>
演示:
<?php
$html = <<<HTML
<div style="text-align:center" width="500" height="300">
<div style="Width : 100px ; Height: 100 px;">
<img src="/images/test.jpg" width=400 height = 200>
<div style="float:left; width: 100px; height : 200 px;"></div>
</div>
<div style="width : 100 px ;height: 100px">
<img src="/images/test.jpg" width=400 height = 200>
</div>
</div>
HTML;
echo '<xmp>';
echo '原內(nèi)容:'.PHP_EOL;
echo $html.PHP_EOL.PHP_EOL;
echo '過濾后內(nèi)容:'.PHP_EOL;
echo clear_wh($html);
echo '</xmp>';
?>
輸出:
原內(nèi)容:
<div style="text-align:center" width="500" height="300">
<div style="Width : 100px ; Height: 100 px;">
<img src="/images/test.jpg" width=400 height = 200>
<div style="float:left; width: 100px; height : 200 px;"></div>
</div>
<div style="width : 100 px ;height: 100px">
<img src="/images/test.jpg" width=400 height = 200>
</div>
</div>
過濾后內(nèi)容:
<div style="text-align:center" >
<div style=" ">
<img src="/images/test.jpg" >
<div style="float:left; "></div>
</div>
<div style="">
<img src="/images/test.jpg" >
</div>
</div>
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php正則表達(dá)式用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- php正則刪除img標(biāo)簽的方法示例
- php正則刪除html代碼中class樣式屬性的方法
- PHP正則刪除html代碼中a標(biāo)簽并保留標(biāo)簽內(nèi)容的方法
- php正則去除網(wǎng)頁中所有的html,js,css,注釋的實現(xiàn)方法
- 使用正則去除php代碼中的注釋方法
- php和editplus正則表達(dá)式去除空白行
- php使用正則表達(dá)式去掉html中的注釋方法
- PHP正則表達(dá)式過濾html標(biāo)簽屬性(DEMO)
- php過濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
- php正則過濾html標(biāo)簽、空格、換行符的代碼(附說明)
- php 正則 過濾html 的超鏈接
相關(guān)文章
PHP實現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
這篇文章主要介紹了PHP實現(xiàn)PDO的mysql數(shù)據(jù)庫操作類,其中dbconfig類負(fù)責(zé)配置數(shù)據(jù)庫訪問信息,dbtemplate類集合了對數(shù)據(jù)庫的訪問操作,非常具有實用價值,需要的朋友可以參考下2014-12-12
php 記錄進(jìn)行累加并顯示總時長為秒的結(jié)果
用php如何將這些記錄進(jìn)行累加,最后顯示為一個總時長為秒鐘的結(jié)果2011-11-11
php判斷某個方法是否存在函數(shù)function_exists (),method_exists()與is_callabl
這篇文章主要介紹了php判斷某個方法是否存在函數(shù)function_exists (),method_exists()與is_callable()區(qū)別與用法,結(jié)合實例形式分析了php function_exists (),method_exists()與is_callable()基本功能、用法、區(qū)別與操作注意事項,需要的朋友可以參考下2020-04-04
php實現(xiàn)在新浪云中使用imagick生成縮略圖并上傳的方法
PHP7.1方括號數(shù)組符號多值復(fù)制及指定鍵值賦值用法分析
php使用preg_match()函數(shù)驗證ip地址的方法

