Skip to content

Instantly share code, notes, and snippets.

@zacwasielewski
Last active August 29, 2015 13:56
Show Gist options
  • Save zacwasielewski/9253346 to your computer and use it in GitHub Desktop.
Save zacwasielewski/9253346 to your computer and use it in GitHub Desktop.
RegEx to convert all relative URLs in an HTML block to absolute URLs
<?php
$content = preg_replace('/(href|src)="((?!(.+:\/\/)|(.+:)|\/).*)"/','\1="/\2"',$content);
# Raw regex: (href|src)="(?!(.+://)|(.+:)|/).*"
@zacwasielewski
Copy link
Author

Uses negative look-arounds to exclude URLs beginning with http://, https://, mailto:, and /

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment