VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

Absolutize Relative Links Using PHP and Preg_Replace_Callback

       

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 w/ #absolute links #file_get_contents #php #preg_replace_callback #regex #relative linksphp