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

查看:48
本文介绍了如何在php的帮助下在每个链接(a 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).

我想要做的是,通过某种方式将每个链接图像添加到 a 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的帮助下在每个链接(a href)标签中添加一个rel属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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