Last time we looked at how PHP could be used to add hyperlinks to text URLs. Now this is something that could be really handy and that we might reuse over and over again. So let's save it as a class. For transparency this will be a separate file called livetext.php and it will be saved in a subfolder called 'classes'. This is what the class looks like in its entirety: <?php class liveText { function addHyperlinksToURLs ($data){ $patterns = array('/(http:\/\/|https:\/\/)/','/([a-zA-Z0-9\-\.]+ \.)(com|co.uk|org.uk|org|net|mil|edu|CO.UK|ORG.UK|COM|ORG|NET|MIL|EDU) (\/.[A-Za-z0-9\.&%\/-]{1,}|)/'); $replace = array('','<a href="http://\0">\0</a>'); echo preg_replace($patterns, $replace, $data); } } ?> It consists of a single function and this does all the work. So that's it for this class. Now when we want to use it, we first of all include it with the line: include ("classes/
Technology tips for writers, editors, designers, illustrators, programmers and publishers, and now Retro Gamers.