Skip to content

Instantly share code, notes, and snippets.

@wderuijter
Last active May 26, 2016 12:23
Show Gist options
  • Save wderuijter/360726d470e200c585bf92621a939ab3 to your computer and use it in GitHub Desktop.
Save wderuijter/360726d470e200c585bf92621a939ab3 to your computer and use it in GitHub Desktop.
//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