2016-02-27 120 views
0

您好,我想用數據庫中的圖像顯示段落。 但我想刪除內聯樣式寬度圖像。以內聯樣式刪除樣式寬度圖像php

例如:

<?php $prg = "hello this is paragragph and this is image 1 
<img src='https://www.google.co.id/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' style='width:150px;heigh:150px'> 
this is text and this is image 2 
<img src='http://www.bing.com/az/hprichbg/rb/HumpbackWhaleReunion_ROW11050338497_1366x768.jpg' style='width:150px;heigh:150px' ></p>"; 

      echo $prg; ?> 

我想在內嵌樣式刪除寬度圖像。

這段代碼的結果,我想這樣的

<?php $prg = "hello this is paragragph and this is image 1 
<img src='https://www.google.co.id/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' style='heigh:150px'> 
this is text and this is image 2 
<img src='http://www.bing.com/az/hprichbg/rb/HumpbackWhaleReunion_ROW11050338497_1366x768.jpg' style='heigh:150px' ></p>"; 

      echo $prg; ?> 
+0

使用style ='heigh:150px; width:0' – devpro

+0

這個數據來自數據庫,我無法改變。 –

+0

如果你得到的數據庫比嘗試: devpro

回答

1

這裏亞走了 - 使用正則表達式

<?php 
$prg = "hello this is paragragph and this is image 1 
<img src='https://www.google.co.id/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' style='width:150px;heigh:150px'> 
this is text and this is image 2 
<img src='http://www.bing.com/az/hprichbg/rb/HumpbackWhaleReunion_ROW11050338497_1366x768.jpg' style='width:150px;heigh:150px' ></p>"; 

echo preg_replace("/width:[0-9]+[a-z]{2};?/i",'',$prg); 
?> 

顯示正則表達式是匹配 https://regex101.com/r/xE8gX7/1

+0

謝謝兄弟的工作:) –

+0

沒問題!請享用!如果我的答案適用於你,請接受我作爲答案!謝謝! –

+0

完成;)brad thank's –

0

您還可以使用str_replace()

$prg = "hello this is paragragph and this is image 1 <img src='https://www.google.co.id/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' style='width:150px;heigh:150px'> this is text and this is image 2 <img src='http://www.bing.com/az/hprichbg/rb/HumpbackWhaleReunion_ROW11050338497_1366x768.jpg' style='width:150px;heigh:150px' ></p>"; 

$prg = str_replace("style='width:150px;heigh:150px'"," style='height:150px;'",$prg);