21,886
社区成员
发帖
与我相关
我的任务
分享
$str = '<img src="1.gif"/>dfdgfdg幅度<img src="asdfasdf/"/>dfdgfdg ';
$str = '<img alt="border=" src="../uploadfile/20120526/20120526171821_52124.jpg" />dfdgfdg幅度';
$pattern = "/<img(.*)src=\"(.+)\".*\/>/isU";
echo preg_replace($pattern, "<a href=\"$2\"><img src=\"$2\" $1 /></a>", $str);
$str = '<img src="1.gif"/>dfdgfdg幅度<img src="asdfasdf/"/>dfdgfdg<img alt="border=" src="../uploadfile/20120526/20120526171821_52124.jpg" /> ';
echo preg_replace('/<img[^>]*src="([^"]+)"[^\/]*\/>/','<a href="\1">\0</a>',$str);
$str = '<img src="1.gif"/>dfdgfdg幅度<img src="asdfasdf/"/>dfdgfdg ';
echo preg_replace('/<img\s+src="([^"]+)"[^\/]*\/>/','<a href="\1">\0</a>',$str);
[User:root Time:13:35:38 Path:/home/liangdong/php]$ php preg.php
<a href="1.gif"><img src="1.gif" border="0"/></a>dfdgfdg幅度<a href="asdfasdf/"><img src="asdfasdf/" border="0"/></a>dfdgfdg [User:root Time:13:35:39 Path:/home/liangdong/php]$ cat preg.php
<?php
$str = '<img src="1.gif"/>dfdgfdg幅度<img src="asdfasdf/"/>dfdgfdg ';
$str = preg_replace('/<img src="(.*)"\/>(.*)<img src="(.*)"\/>((?-U)\S+)/iUs', '<a href="\1"><img src="\1" border="0"/></a>\2<a href="\3"><img src="\3" border="0"/></a>\4' ,$str);
print_r($str);
?>