不要使用正则表达式来解析 HTML!
是的,一般来说,使用正则表达式来解析 HTML 充满了危险。计算机科学家会正确地指出 HTML 不是REGULAR语言。然而,与这里许多人认为的相反,在某些情况下使用正则表达式解决方案是完全有效和适当的。阅读 Jeff Atwoods 关于这个主题的博客文章:解析 Html 的克苏鲁方式。撇开免责声明不谈,让我们继续使用正则表达式解决方案......
问题重述:
原来的问题相当模糊。这是对问题的更精确的解释/重新表述(可能根本不是OP所要求的):
Given:我们有一些 HTML 文本(HTML 4.01 or XHTML 1.0)。这段文字包含<A..>...</A>
锚元素。其中一些锚元素是图像文件资源的链接(即HREF
属性指向以文件扩展名结尾的 URI:JPEG
, JPG
, PNG
or GIF
)。其中一些图像链接是简单的文本链接,其中锚元素的内容是纯文本,没有其他 HTML 元素,例如<a href="picture.jpg">Link text with no HTML tags</a>
.
Find:是否有正则表达式解决方案可以采用这些“纯文本链接到图像资源文件”链接,并将链接文本替换为IMG
元素具有SRC
属性设置为相同的图像 URI 资源?以下(有效的 HTML 4.01)示例输入包含三个段落。第一段中的所有链接都要修改,但第二段和第三段中的所有链接都不要修改并保持原样:
HTML 输入示例:
<p title="Image links with plain text contents to be modified">
This is a <a href="img1.png">LINK 1</a> simple anchor link to image.
This <a title="<>" href="img2.jpg">LINK 2</a> has attributes before HREF.
This <a href="img3.gif" title='<>'>LINK 3</a> has attributes after HREF.
</p>
<p title="NON-image links with plain text contents NOT to be modified">
This is a <a href="tmp1.txt">LINK 1</a> simple anchor link to NON-image.
This <a title="<>" href="tmp2.txt">LINK 2</a> has attributes before HREF.
This <a href="tmp3.txt" title='<>'>LINK 3</a> has attributes after HREF.
</p>
<p title="Image links with NON-plain text contents NOT to be modified">
This is a <a href="img1.png"><b>BOLD 1</b></a> anchor link to image.
This is an <a href="img3.gif"><img src="img3.gif"/></a> image link to image.
</p>
所需的 HTML 输出:
<p title="Image links with plain text contents to be modified">
This is a <a href="img1.png"><img src="img1.png" /></a> simple anchor link to image.
This <a title="<>" href="img2.jpg"><img src="img2.jpg" /></a> has attributes before HREF.
This <a href="img3.gif" title='<>'><img src="img3.gif" /></a> has attributes after HREF.
</p>
<p title="NON-image links with plain text contents NOT to be modified">
This is a <a href="tmp1.txt">LINK 1</a> simple anchor link to NON-image.
This <a title="<>" href="tmp2.txt">LINK 2</a> has attributes before HREF.
This <a href="tmp3.txt" title='<>'>LINK 3</a> has attributes after HREF.
</p>
<p title="Image links with NON-plain text contents NOT to be modified">
This is a <a href="img1.png"><b>BOLD 1</b></a> anchor link to image.
This is an <a href="img3.gif"><img src="img3.gif"/></a> image link to image.
</p>
请注意,这些示例包括测试用例<A..>...</A>
锚标记在所需的 HREF 属性之前和之后都有单引号和双引号属性值,并且包含 cthulhu 诱人的(但完全有效的 HTML 4.01)、尖括号。
另请注意,替换文本是一个以以下结尾的(空)IMG 标签:'/>'
(这不是有效的 HTML 4.01)。
正则表达式解决方案:
问题的陈述定义了一个highly具体要匹配的模式有以下要求:
- The
<A..>...</A>
开始标签之前和/或之后可以有任意数量的属性HREF
属性。
- The
HREF
属性值必须具有以以下结尾的值JPEG
, JPG
, PNG
or GIF
(不区分大小写)。
- 的内容
<A..>...</A>
元素不得包含任何其他 HTML 标签。
- The
<A..>...</A>
元素目标模式不是嵌套结构。
当处理如此高度特定的子字符串时,精心设计的正则表达式解决方案可以很好地工作(很少有边缘情况会导致问题)。这是一个经过测试的 PHP 函数,它可以很好地完成工作(并正确转换上面的示例输入):
// Convert text-only contents of image links to IMG element.
function textLinksToIMG($text) {
$re = '% # Match A element with image URL and text-only contents.
( # Begin $1: A element start tag.
<a # Start of A element start tag.
(?: # Zero or more attributes before HREF.
\s+ # Whitespace required before attribute.
(?!href\b) # Match attributes other than HREF.
[\w\-.:]+ # Attribute name (Non-HREF).
(?: # Attribute value is optional.
\s*=\s* # Attrib name and value separated by =.
(?: # Group for attrib value alternatives.
"[^"]*" # Either double quoted,
| \'[^\']*\' # or single quoted,
| [\w\-.:]+ # or unquoted value.
) # End group of value alternatives.
)? # Attribute value is optional.
)* # Zero or more attributes before HREF.
\s+ # Whitespace required before attribute.
href\s*=\s* # HREF attribute name.
(?| # Branch reset group for $2: HREF value.
"([^"]*)" # Either $2.1: double quoted,
| \'([^\']*)\' # or $2.2: single quoted,
| ([\w\-.:]+) # or $2.3: unquoted value.
) # End group of HREF value alternatives.
(?<= # Look behind to assert HREF value was...
jpeg[\'"] # either JPEG,
| jpg[\'"] # or JPG,
| png[\'"] # or PNG,
| gif[\'"] # or GIF,
) # End look behind assertion.
(?: # Zero or more attributes after HREF.
\s+ # Whitespace required before attribute.
[\w\-.:]+ # Attribute name.
(?: # Attribute value is optional.
\s*=\s* # Attrib name and value separated by =.
(?: # Group for attrib value alternatives.
"[^"]*" # Either double quoted,
| \'[^\']*\' # or single quoted,
| [\w\-.:]+ # or unquoted value.
) # End group of value alternatives.
)? # Attribute value is optional.
)* # Zero or more attributes after HREF.
\s* # Allow whitespace before closing >
> # End of A element start tag.
) # End $1: A element start tag.
([^<>]*) # $3: A element contents (text-only).
(</a\s*>) # $4: A element end tag.
%ix';
return preg_replace($re, '$1<img src="$2" />$4', $text);
}
是的,这个解决方案中的正则表达式很长,但这主要是由于广泛的注释,这也使得它高度可读。它还可以正确处理可能包含尖括号的带引号的属性值。是的,当然可以创建一些 HTML 标记来破坏这个解决方案,但是这样做所需的代码将非常复杂,几乎是闻所未闻的。