用php程序把原来html代码中的图片加上超链接

大川韬滔 2015-12-08 11:32:12
有段文本如下:
<p>物品尺寸/规格:高24厘米,,好东西值得收藏。</p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/></p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157109272.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="2.jpg"/></p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157112075.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="3.jpg"/></p><p><br/></p>

我想把下段代码中的图片加上一个超链接,比如把
<img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/>

换成:
<a href="/data/attachment/antique//20141123/14167157082579.jpg" target="_blank">
<img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/>
</a>

在php程序中应该怎么处理呢?
...全文
354 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
智商众筹 2015-12-09
  • 打赏
  • 举报
回复
你还是再看看手册吧
xuzuning 2015-12-08
  • 打赏
  • 举报
回复
$s =<<< TXT
<p>物品尺寸/规格:高24厘米,,好东西值得收藏。</p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/></p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157109272.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="2.jpg"/></p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157112075.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="3.jpg"/></p><p><br/></p>
TXT;

$s = preg_replace('#<img.+src=".+(/data/.+)".+/>#iU', '<a href="$1" target="_blank">$0</a>', $s);
<p>物品尺寸/规格:高24厘米,,好东西值得收藏。</p><p><a href="/data/attachment/antique//20141123/14167157082579.jpg" target="_blank"><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/></a></p><p><a href="/data/attachment/antique//20141123/14167157109272.jpg" target="_blank"><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157109272.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="2.jpg"/></a></p><p><a href="/data/attachment/antique//20141123/14167157112075.jpg" target="_blank"><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157112075.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="3.jpg"/></a></p><p><br/></p>
大川韬滔 2015-12-08
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
$0、$1 .... 是正则表达式向后引用 表示第几个匹配项
感谢大师指点! #与#iU又代表什么意思?
xuzuning 2015-12-08
  • 打赏
  • 举报
回复
$0、$1 .... 是正则表达式向后引用 表示第几个匹配项
大川韬滔 2015-12-08
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
没有学过正则表达式? 那么至少要看看手册中的描述吧
看过,但是在执行preg_replace()函数钱,$1、$0已经已经被赋值了,可这里没有呀。所以怎么利用$s给$1、$0赋值呢?
xuzuning 2015-12-08
  • 打赏
  • 举报
回复
没有学过正则表达式? 那么至少要看看手册中的描述吧
大川韬滔 2015-12-08
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
$s =<<< TXT
<p>物品尺寸/规格:高24厘米,,好东西值得收藏。</p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/></p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157109272.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="2.jpg"/></p><p><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157112075.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="3.jpg"/></p><p><br/></p>
TXT;

$s = preg_replace('#<img.+src=".+(/data/.+)".+/>#iU', '<a href="$1" target="_blank">$0</a>', $s);
<p>物品尺寸/规格:高24厘米,,好东西值得收藏。</p><p><a href="/data/attachment/antique//20141123/14167157082579.jpg" target="_blank"><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157082579.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="1.jpg"/></a></p><p><a href="/data/attachment/antique//20141123/14167157109272.jpg" target="_blank"><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157109272.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="2.jpg"/></a></p><p><a href="/data/attachment/antique//20141123/14167157112075.jpg" target="_blank"><img src="/ueditor/php/../../data/attachment/antique//20141123/14167157112075.jpg" style="max-width:700px;width:expression(this.width > 700? 700: true);" title="3.jpg"/></a></p><p><br/></p>
$1、$0别人代表什么哟?

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧