如何在php的帮助下在每个链接(一个href)标签中添加一个rel属性? [英] How to add in every link (a href) tag a rel attribute with the help of php?

查看:169
本文介绍了如何在php的帮助下在每个链接(一个href)标签中添加一个rel属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量$ mydata(我的代码在php 中),其中包含文本和一些图片。包含的每张图片都采用以下格式:

I have a variable $mydata (my code is in php) which includes text and some pictures. Every picture included is in this form :

<img src="mypicture.jpg" alt="lala" title="lala" border="0" />

此变量$ mydata将文本和图像插入到我的数据库(mysql)中。

This variable $mydata which has text and images after is inserted into my database (mysql).

我想做的是,通过某种方式将每个和每个链接图像添加到一个href标签中,在img标签之前,还有一个属性,这个是rel =lightbox,以便在我的所有图像中应用灯箱效果,例如:

What i want to do is, with some way to add to every and each one of the link images into the a href tag, before the img tag, one more attribute and this is rel="lightbox" so that lightbox effect with be applied in all of my images, such as :

<a href="mypicture_fullsize.jpg" rel="lightbox"><img src="mypicture.jpg" rel="lightbox" alt="lala" title="lala" border="0" /></a>

如何在php中制作这个,能帮帮我吗?

How to make this in php, can you help me please ?

我想我们可能必须使用某种正则表达式来实现它,但我对正则表达式的了解不是那么提前..你能帮帮我吗?

I guess may we have to use some kind of regular expression in order to implement that but my knowledge of regular expressions is not so advance.. can you help me please ?

推荐答案

$pat = <<<pattern
~(<a href="[^"]+")(><img src="[^"]+" alt="[^"]+" title="[^"]+" border="[^"]+" /></a>)~
pattern;
$html = preg_replace($pat, "\\1 rel=\"lightbox\"\\2", $html);

这篇关于如何在php的帮助下在每个链接(一个href)标签中添加一个rel属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆