Last active
May 26, 2016 12:23
-
-
Save wderuijter/360726d470e200c585bf92621a939ab3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Some HTML | |
$page = "<html><body><a href="/some/link.html">Link1</a><a href="/another/link">Link2</a></body></html>"; | |
//the absolute baseURL (hardcoded test) | |
$baseurl="https://www.example.com"; | |
// Make the URLS absolute | |
function expand_links($link) { | |
// add the base URL | |
return('href="'.$baseurl''.trim($link[0], '\'"/\\').'"'); | |
} | |
$newpage = preg_replace_callback('/href\s*=\s*(?<href>"[^\\"]*"|\'[^\\\']*\')/', 'expand_links', $page); | |
/* result should be | |
<html><body><a href="https://www.example.com/some/link.html">Link1</a><a href="https://www.example.com/another/link">Link2</a></body></html> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment