I have an HTML document. I need to replace the url relative to absolute.
BaseUrl : https://www.example.com/example/
For example
/*different types of relative URL that I want to replace with absolute URL*/
<img src="/relative/url/img.jpg" />
<img src="./relative/url/img.jpg" />
<img src="../relative/url/img.jpg" />
<a src="../../relative/url/img.jpg" /></a>
<a href='relative/url/'>example</a>
/*url i don't want them to change*/
<img src="//example.com/img.jpg" />
<img src="http://example.com/img.jpg" />
<img src="https://example.com/img.jpg" />
Desired Output would be:
<img src="https://www.example.com/example/relative/url/img.jpg" />
<img src="https://www.example.com/example/relative/url/img.jpg" />
<img src="https://www.example.com/example/relative/url/img.jpg" />
<a src="https://www.example.com/example/relative/url/img.jpg" /></a>
<a href='https://www.example.com/example/relative/url/'>example</a>
But I have to account for single and double quoted attribute values for src, href, and action
Right now I am trying like this preg_replace:
preg_replace('~(?:src|action|href)=[\'"]\K../(?!/)[^\'"]*~',"$baseUrl$0",$html);
But it only works with some URLs. What I want is for them to work with all the relative URLs that I mentioned earlier.
Thanks for the help.