Regex to replace relative link with root relative link

Posted by Kendall Hopkins on Stack Overflow See other posts from Stack Overflow or by Kendall Hopkins
Published on 2010-05-19T22:17:00Z Indexed on 2010/05/19 22:20 UTC
Read the original article Hit count: 577

Filed under:
|
|
|
|

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.

Replaced links:

<tag ... href="path/to_file.ext" ... >   --->   <tag ... href="/basepath/path/to_file.ext" ... >

Untouched links:

<tag ... href="/any/path" ... >
<tag ... href="protocol://domain.com/any/path" ... >

© Stack Overflow or respective owner

Related posts about php

Related posts about regex