2016-02-27 66 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);