简体   繁体   English

正则表达式使用href包含src包装img标签

[英]regex to wrap img tag with href containg the src

[Edited - Sorry Bart] I've looked at other answers but struggling to match this. [编辑-对不起,巴特]我看过其他答案,但努力做到这一点。 I want to wrap an image tag where the src is the second attribute (after title) with a specific anchor tag that contains a link to the image found in the src from the image tag. 我想包装一个图像标签,其中src是第二个属性(在标题之后),并带有一个特定的锚标签,该锚标签包含一个指向从图像标签在src中找到的图像的链接。

Example of img tag in string. 字符串中的img标签示例。 This has been entered via tinymce wysiwyg and always adds title then src. 这是通过tinymce wysiwyg输入的,总是添加标题,然后添加src。

<img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" alt="who_main_Layer_1.jpg" width="380" height="268" />

I need to take all of these and wrap with the following href: 我需要将所有这些都考虑在内并包装以下href:

<a href="event:images/expand/image.jpg"><img src=”images/image.jpg” /></a>

The image src points to the thumbnail and the (Flash AS3 Event) pops up the full size version. 图像src指向缩略图,并且(Flash AS3事件)弹出完整尺寸的版本。 Both images named the same just different folders. 两个图像都命名相同,只是文件夹不同。

Here is a full example of a string that would need the regex running against (Due to sensitive data I've substituted text for Lorem ipsum, but the layout is the same!): 这是一个需要正则表达式运行的字符串的完整示例(由于敏感数据,我已将文本替换为Lorem ipsum,但布局相同!):

<p>Lorem ipsum dolor sit amet</p>
<p>&nbsp;</p>
<p>Lorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit  
ametLoremipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem 
ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit amet</p>
<p>&nbsp;</p>
<p><img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" 
alt="who_main_Layer_1.jpg" width="380" height="268" /></p>
<p>&nbsp;</p>
<p>Lorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem 
ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum 
dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor 
sit  
ametLorem ipsum dolor sit ametLorem ipsum dolor sit amet</p>
<p>&nbsp;</p>
<p><img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" 
alt="who_main_Layer_1.jpg" width="380" height="268" /></p>`

Many thanks in advance, Marc 预先感谢,马克

Similar questions have been answered several times and the answer is always the same: do not use regular expressions to tamper with HTML . 类似的问题已经回答了好几次,答案总是相同的: 不要使用正则表达式来篡改HTML In PHP, you can use XPath and the SimpleXml or DOMParser extensions to solve this problem. 在PHP中,可以使用XPathSimpleXmlDOMParser扩展来解决此问题。

Sorry for posting so many links to my own answers but the answers themselves and the questions they are answering contain a lot of information on the subject. 很抱歉张贴了这么多我自己的答案的链接,但答案本身以及他们正在回答的问题包含有关该主题的很多信息。

Try this code: 试试这个代码:

<?php
$str = '<img title="who_main_Layer_1.jpg" src="../../images/who_main_Layer_1.jpg" alt="who_main_Layer_1.jpg" width="380" height="268" />';

preg_match('#src="(?:.*/)?(.*?)"#', $str, $match);
$src = $match[1];
?>
<a href="event:images/expand/<?php echo $src; ?>"><img src=”images/<?php echo $src; ?>” /></a>

EDIT: another version to account for multiple tags in the string: 编辑:考虑到字符串中多个标签的另一个版本:

$replace = '<a href="event:images/expand/$1"><img src="images/$1" /></a>';
$str = preg_replace('#<\s*img.*?src="(?:[^"]+/)?(.*?)".*?>#s', $replace, $str);

Try this : 尝试这个 :

$newString = preg_replace('`<img([^>]*)src="\\.\\./\\.\\./images/([^"]+)"([^>])*>`','<a href="event:images/expand/$2"><img$1src="images/$2"$3></a>', $oldString);

Limitations are : 局限性是:

  • It will apply the changes in things like <input value='<img src="../../images/test.jpg/>"'/> 它将应用诸如<input value='<img src="../../images/test.jpg/>"'/>类的更改
  • If " are replaced by ' in your img tags, you'll have to change the regexp 如果在img标签中将"替换为' ,则必须更改正则表达式
  • It will choke on things like <img alt="6>5" src="../../images/test.png"/> 它将使类似<img alt="6>5" src="../../images/test.png"/>

I agree with other commenters saying regexp are bad to parse HTML. 我同意其他评论者的观点,称正则表达式不好解析HTML。 But there's almost no parsing here and the format of things to replace seems to be under control (generated by tinymce). 但是这里几乎没有解析,替换内容的格式似乎在控制之下(由tinymce生成)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM