I fixed them all. I treat every word as an element of an array
It works perfect
function format_links($chk_str){
$counter=0;
$final_arrays = array();
$large_array = explode("<br>", $chk_str);
foreach($large_array as $line){
$final_arrays[] = explode(" ", $line);
$counter++;
}
$j=0;
for($i=0; $i<$counter; $i++){
foreach($final_arrays[$i] as $word)
{
// $b = preg_replace("#([url]http://[/url]([^\s]+))|(www\.[^\s]+)#i", "<a href=\"http://$2$3\">$2$3</a>", $final_arrays[$i]);
$allthestring[$i][$j]=preg_replace("#([url]http://[/url]([^\s]+))|(www\.[^\s]+)#i", "<a href=\"http://$2$3\" target=_blank>$2$3</a>", $word);
if($allthestring[$i][$j]==$word) {
$pos1 = strpos($word, "@");
$pos2 = strrpos($word, ".");
// if($pos1&&($pos2<=(strlen($word)-3))){
if(($pos1&&$pos2)&&$pos2<=(strlen($word)-3)){
$email = true;
} else {
$email = false;
}
if($email){
$allthestring[$i][$j] = "<a href='mailto:$word'>$word</a>";
}
}
$j++;
}
$j=0;
}
foreach($allthestring as $inner_array){
$inner_strings[] = join(" ", $inner_array);
}
$new_large_string = join("<br>", $inner_strings);
// $chk_str = implode(" ", $a);
// $chk_str=preg_replace("#([url]http://[/url]([^\s]+))|(www\.[^\s]+)#i", "<a href=\"http://$2$3\" target=_blank>$2$3</a>", $new_large_string);
return $new_large_string;
//return $chk_str;
}