Written by Sean Behan on Sun Jun 17th 2012

I was in the market for a simple php script to replace hrefs with their absolute paths from scraped web pages. I wrote one myself. I used the preg_replace_callback function so that I could pass the parsed results as a single variable.

<?php
$domain = "http://seanbehan.com";
$pattern = "/\bhref=[\"|'](.*?)[\"|']/";
$string = file_get_contents($domain);

// prepends relative links w/ $domain skips returns the match if already absolute function replace_href($match){ global $domain; if(substr($match[1], 0, 7)!=="http://" && substr($match[1],0,8)!=="https://"){ return "href='".$domain.$match[1]."'"; } else { return "href='".$match[1]."'s"; } } print preg_replace_callback($pattern, "replace_href", $string);


Tagged with..
#absolute links #file_get_contents #php #preg_replace_callback #regex #relative links #php

Just finishing up brewing up some fresh ground comments...