html Regex将link标记转换为img标记

zynd9foi  于 9个月前  发布在  其他
关注(0)|答案(4)|浏览(70)

我需要一个正则表达式来找到所有出现(可能是多个)的标签的文本:“Graphicsource”,并将其转换为带有src属性的img标记,该属性包含hrefurl。
所以从

<small><a href="https://www.url.com/image.png" target="_blank" rel="noopener">Graphic source</a></small>

<img src="https://www.url.com/image.png"/>

例如:

Some text
Other tag <b>test</b>
<small><a href="https://www.url.com/name1.png" target="_blank" rel="noopener">Graphic source</a></small>test
<small><a href="https://www.url.com/name2.jpg" target="_blank" rel="noopener">Graphic source</a></small>Text text<small><a href="www.url.com">Do not transform</a></small>

需要转换为:

Some text
Other tag <b>test</b>
<img src="https://www.url.com/name1.png"/>test
<img src="https://www.url.com/name2.jpg"/>Text text<small><a href="www.url.com">Do not transform</a></small>

我几乎让它工作:<small.*?href="(.*?)"
我不明白如何不包括一个标签,不包含文字图形来源作为文本,以及如何不包括所有其他属性的一个标签时,转换为img标签。
https://regex101.com/r/OReOCd/1

yhived7q

yhived7q1#

不要使用regex解析HTML/XML

检查:

最好使用编程语言和适当的库来解析HTML

使用最常用的语言之一Python:

import requests
from lxml import html

res = requests.get('https://sputnick.fr/downloads/regex-to-transform-link-tag-to-img-tag.html')
tree = html.fromstring(res.text)
    
# Using proper XPath query language:
elts = tree.xpath('//a[text()="Graphic source"]')

for a_elt in elts:
    img_elt = html.Element("img", src=a_elt.get("href"))
    a_elt.getparent().replace(a_elt, img_elt)

transformed_html = html.tostring(tree, encoding="unicode")

print(transformed_html)

或者PHP,使用DOMXPath

<?php
    $html = file_get_contents('https://sputnick.fr/downloads/regex-to-transform-link-tag-to-img-tag.html');
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    
    // Using proper XPath query language:
    $elements = $xpath->query('//a[text()="Graphic source"]');
    
    foreach ($elements as $element) {
        $href = $element->getAttribute('href');
        $imgElement = $dom->createElement('img');
        $imgElement->setAttribute('src', $href);
        $element->parentNode->replaceChild($imgElement, $element);
    }
    
    $transformedHtml = $dom->saveHTML();
    
    echo $transformedHtml;
?>

输出

<html lang="en">
  <head>
    <title>Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
  </head>
  <body>
Some text
Other tag <b>test</b>
<small><img src="https://www.url.com/name1.png"></small>test
<small><img src="https://www.url.com/name2.jpg"></small>Text text<small><a href="www.url.com">Do not transform</a></small>
  </body>
</html>
d5vmydt9

d5vmydt92#

“.我需要一个正则表达式来找到所有的事件.将[them]转换为带有src属性的img标签,该属性包含href url。...”
正则表达式模式本身不会替换任何值,它只是匹配。
您需要使用程序或编程语言。

  • "...我不明白如何不包括不包含文字图形源作为文本的标签..."*

声明>后面的文本为 “Graphic source<"

<.+?href\s*=\s*("|')(.+?)(?<!\\)\1.+?>Graphic source<.+>

替换文本将是,

<img src="$2"/>

此外,我假设您可以在文本之前和之后使用\s*

<.+?href\s*=\s*("|')(.+?)(?<!\\)\1.+?>\s*Graphic source\s*<.+>
  • "...以及如何在转换为img标签时不包含a标签的所有其他属性。..."*

在这种类型的情况下,有重复的键和值,您可以使用 lazy-quantifier?来匹配第一个遇到的引号。
比如说,

=\"(.+?)\"

下面是一个示例输出

Some text
Other tag <b>test</b>
<img src="https://www.url.com/name1.png"/>test
<img src="https://www.url.com/name2.jpg"/>
4szc88ey

4szc88ey3#

强制性免责声明:Stop Parsing (X)HTML with Regular Expression

<small><a href="(.*?)"[^>]*?>Graphic source<\/a><\/small>

https://regex101.com/r/2Wd9le/1

yjghlzjz

yjghlzjz4#

这应该做的工作:

<small><a href="(https?://[^"]+)"[^>]+>Graphic source<\/a><\/small>

对于您的替代品,您可以执行以下操作:

<img src="$1"/>

相关问题