php 正则表达式替换与正则替换函数
php 正则表达式替换与正则替换函数 /* 下面我们是要把指定正则出来的内空替换成别一种内容,这样做我们就会要用到正则替换函数preg_replace了,下面的实例是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);
PHP之友评论