How to add in every link (a href) tag a rel attribute with the help of php?(如何在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="bXlwaWN0dXJlLmpwZw==" alt="bGFsYQ==" title="bGFsYQ==" 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="bXlwaWN0dXJlLmpwZw==" rel="lightbox" alt="bGFsYQ==" title="bGFsYQ==" 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="W14="]+" alt="W14="]+" title="W14="]+" border="[^"]+" /></a>)~
pattern;
$html = preg_replace($pat, "\1 rel="lightbox"\2", $html);
这篇关于如何在php的帮助下在每个链接(a href)标签中添加一个rel属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在php的帮助下在每个链接(a href)标签中添加一个rel属性?


- 没有作曲家的 PSR4 自动加载 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Laravel 仓库 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01