我有一个包含所有不同类型的链接(相对,绝对,根相对)的HTML文本的字符串。我需要一个可以由PHP的preg_replace
执行的正则表达式来替换所有相关链接和根相关链接,而不会触及任何其他链接。我已经有了根路径。正则表达式来替换相对链接与根相对链接
替换链接:
<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" ... />
它必须是正则表达式吗?使用DOM类可以提供更准确的结果。 – webbiedave 2010-05-19 22:28:01
它必须是正则表达式,因为我不能保证DOM对于任何基本的DOM阅读器都能够正确解析有效。 – 2010-05-19 22:32:34