Version: 4
Type: Function
Category: Other
License: GNU General Public License
Description: This function can be used to format URLs and E-Mail addresses as hyperlinks when they are being outputted.
<?
function format_links($chk_str){
$a = explode(" ", $chk_str);
for($i=0; $i<count($a); $i++){
//Take care of URLs that don't start with "http://"
$b = str_replace("www.", "http://www.", $a[$i]);
$b = str_replace("http://http://", "http://", $b);
//Identify URL
$pos = strpos($b, "http://");
if ($pos === false) { // note: three equal signs
$url_found = false;
} else {
$url_found = true;
}
if($url_found){
//Replace URL with formatted URL
$a[$i] = "<a href='$b' target=_blank>$b</a>";
}
//Identify E-Mail
$pos = strpos($b, "@");
if($pos === false){
$email = false;
} else {
$email = true;
}
if($email){
//Replace e-mail with formatted e-mail
$a[$i] = "<a href='mailto:$b'>$b</a>";
}
}
//Put string toghet with formatted links
$chk_str = implode(" ", $a);
//Return formatted string
return $chk_str;
}
?>