正则表达式用根相对链接替换相对链接 [英] Regex to replace relative link with root relative link
问题描述
我有一个包含所有不同类型链接(相对,绝对,根相对)的html的文本字符串。我需要一个可以由PHP的 preg_replace
执行的正则表达式,用相对链接替换所有相对链接,而不触及任何其他链接。我已经有了根路径。
I have a string of text that contains html with all different types of links (relative, absolute, root-relative). I need a regex that can be executed by PHP's preg_replace
to replace all relative links with root-relative links, without touching any of the other links. I have the root path already.
替换了链接:
<tag ... href="path/to_file.ext" ... > ---> <tag ... href="/basepath/path/to_file.ext" ... >
<tag ... href="path/to_file.ext" ... /> ---> <tag ... href="/basepath/path/to_file.ext" ... />
未触动的链接:
<tag ... href="/any/path" ... >
<tag ... href="/any/path" ... />
<tag ... href="protocol://domain.com/any/path" ... >
<tag ... href="protocol://domain.com/any/path" ... />
推荐答案
如果您只想更改基本URI,可以尝试 BASE
元素:
If you just want to change the base URI, you can try the BASE
element:
<base href="/basepath/">
但请注意,更改基URI会影响所有相对URI,而不仅仅是相对URI路径。
But note that changing the base URI affects all relative URIs and not just relative URI paths.
否则,如果您真的想使用正则表达式,请考虑您想要的相对路径必须是 path-noscheme <的类型/ em>(参见 RFC 3986 ):
Otherwise, if you really want to use regular expression, consider that a relative path like you want must be of the type path-noscheme (see RFC 3986):
path-noscheme = segment-nz-nc *( "/" segment )
segment = *pchar
segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
; non-zero-length segment without any colon ":"
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
pct-encoded = "%" HEXDIG HEXDIG
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
因此URI的开头必须匹配:
So the begin of the URI must match:
^([a-zA-Z0-9-._~!$&'()*+,;=@]|%[0-9a-fA-F]{2})+($|/)
但是请使用适当的HTML解析器来解析HTML,从而构建一个DOM。然后,您可以查询DOM以获取 href
属性,并使用上面的正则表达式测试该值。
But please use a proper HTML parser for parsing the HTML an build a DOM out of that. Then you can query the DOM to get the href
attributes and test the value with the regular expression above.
这篇关于正则表达式用根相对链接替换相对链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!