删除内联样式php中的样式宽度图像

时间:2016-02-27 04:35:30

标签: php html css

你好我想用数据库中的图像显示段落。 但我想删除内联样式宽度图像。

示例:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);