首页 > php基础 > php 正则表达式替换与正则替换函数

php 正则表达式替换与正则替换函数

下面我们是要把指定正则出来的内空替换成别一种内容,这样做我们就会要用到正则替换函数preg_replace了,下面的实例是

文本有下面标签

 代码如下 复制代码

<img style src="<?=$url?>/images/styleno.jpg" width="30" height="30" />
<img style src="<?=$url?>/images/styleno.jpg" width="30" height="30" />
<img src="images/styleno.jpg" width="30" height="30" />
<img src="images/styleno.jpg" width="30" height="30" />
想用正则把他们替换成
有 style的 替换成<img src="<?=$url?>/images/styleno.jpg" width="30" height="30" />
没style 的 替换成<img src="<?=$path?>/images/styleno.jpg" width="30" height="30" />

*/

$content1 ='<img style src="<?=$url?>/images/styleno.jpg" width="30" height="30" />';
$content ='<img style src="/images/styleno.jpg" width="30" height="30" />';
$a='<img src="<?=$url?>/images/styleno.jpg" width="30" height="30" />';
echo preg_replace('/<imgs+styles+src=['"]?[^'"]*['"]?s+.*/>/i',$a,$content);

$content1 ='<img src="images/styleno.jpg" width="30" height="30" />';
$b='<img src="<?=$path?>/images/styleno.jpg" width="30" height="30" />';
echo preg_replace('/<img src=['"]?[^'"]*['"]?s+.*/>/i',$b,$content1);



本文地址:http://www.phprm.com/base/34335.html

转载随意,但请附上文章地址:-)

标签:none

发表留言