dooid3005
dooid3005
2016-02-27 04:35

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

已采纳

hello i want show paragraph with image from database. but i want delete inline style width image.

example :

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

i want delete width image in inline style.

this code result i want like this

   <?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条回答

  • dqnf28092 dqnf28092 5年前

    here ya go - using regular expressions

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

    showing regex is matching https://regex101.com/r/xE8gX7/1

    点赞 评论 复制链接分享
  • douzhu3367 douzhu3367 5年前

    You can also use 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); 
    
    点赞 评论 复制链接分享

相关推荐