<?php

// CREATE THE STRING
$urls_as_string = 'asd a http://w...content-available-to-author-only...e.com w223 http://e...content-available-to-author-only...e.com/  ion
sipn  http://w...content-available-to-author-only...e.com/seconday/somepage#hashes?parameters 



opajiw348283 http://w...content-available-to-author-only...e.com/seconday/ 20923[\'#$%#$ http://w...content-available-to-author-only...e.com/seconday ww
we http://e...content-available-to-author-only...e.com/seconday               http://e...content-available-to-author-only...e.com/seconday/ 000000
02222 http://d...content-available-to-author-only...d.com/personal-wordpress-blog-themes/ +_)(&^&%$ https://c...content-available-to-author-only...t.com/nikokolev/7993-Kubrat-Responsive-Template oopeo
rop  www.tuicool.com/articles/rqAzU3 03083 2h1hh1`  html5up.net/overflow/ kksllkwpo2 http://w...content-available-to-author-only...l.com/articles/rqAzU3  la;s
l2i2i3okn2 http://l...content-available-to-author-only...m.au/spotfinder/docs/#ByVCPlik black cat www.designrazzi.com/2013/free-css3-html5-templates/ as
df themeko.org/halsey-v1-1-9-ultimate-business-wordpress-theme/ l 
 www <a href="http://w...content-available-to-author-only...e.com/questions/124977/how-to-add-qtranslate-multi-language-support-for-media/131971#131971" target="_blank">SE</a>';


// SET SOME DOMAIN EXTENSIONS
$extension_array = array('com', 'net', 'org', 'biz');



// CHECK TO SEE IF OUR REGEX IS WORKING ... PRINT OUT ALL OF THE MATCHES
if (preg_match_all('~(?:(?:http(?:s)?://)?(?:www\.)?[-A-Z0-9.]+(?:\.'.implode('|', $extension_array).')[-A-Z0-9_./]?(?:[-A-Z0-9#?/]+)?)~ims', $urls_as_string, $m)) {
    print_r($m);
}



// USE PREG_REPLACE_CALLBACK TO FORMAT THE URLS
$content = preg_replace_callback( '~(?:(?:http(?:s)?://)?(?:www\.)?[-A-Z0-9.]+(?:\.'.implode('|', $extension_array).')[-A-Z0-9_./]?(?:[-A-Z0-9#?/]+)?)~ims', 'my_callback', $urls_as_string);



// PRINT OUT THE FINISHED STRING
print "\n\n\n\nFINAL OUTPUT: \n".$content;



// THIS FUNCTION DOES A CRAPTASTIC JOB AT FORMATTING URLS
function my_callback($m) {

	$url = $m[0];
	$url_formatted = $url;

	if (!preg_match('~^http(s)?://~', $url)) {
		$url_formatted = 'http://'.$url;
	}

	$url_formatted = '<a href="'.$url.'">'.$url.'</a>';

	return $url_formatted;

}