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="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属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在php的帮助下在每个链接(a href)标签中添加一个rel属性?
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
