DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

My name is Taylor Hawkes and I have been developing websites and web applications for over 6 years now. I like running across difficult problems, and finding creative ways to overcome them. I’ve noticed I excel at my work when I enjoy it, and I love a challenge. Mysteries fascinate me, and I find technology to be the biggest one of all, the more I understand the more eager I become to know more. I am strong believer in continually acquiring knowledge, its seems as though with every piece comes an opportunity or a better way of doing things.I am an entrepreneur at heart, and I have started a few businesses, I now run Woodstitch. Outside of work, I stay busy with with mostly outdoor activities; growing up in North Carolina with three brothers made sure of that. I am currently training for a half-marathon and hope to do a full one not long after. I graduated from Appalachian State University, so naturally I enjoy hiking, camping and many sports, lacrosse being my favorite (like ice hockey, but the sticks have a net and the puck is a ball, for those from the west coast). If I am not outside, I love being with friends and family, reading, or watching a little Seinfeld. Taylor has posted 3 posts at DZone. You can read more from them at their website. View Full User Profile

Replace relative url's with absolute

07.11.2013
| 3638 views |
  • submit to reddit

<?php

function replace_relative_urls($html) {

$replace='$1http://mywebsite.com/$2$3';
$pattern="#(<\s*?a\s*?[^>]*href\s*?=[\"'])(?!http)([^\"'>]+)([\"'>]+)#";
$html=preg_replace($pattern,$replace, $html);
return $html;

}

This snippet will replace all anchor that have relative url's to be absolute to whatever domain you provide. I use it often so I thought I would share.