I have made this links converting function that puts url in <a></a>
tags:
function convert_links($text) {
$text = explode(' ',$text);
for($i=0;$i<count($text);$i++) {
if(filter_var($text[$i], FILTER_VALIDATE_URL))
$text[$i] = "<a href='".$text[$i]."' target='_blank'>".$text[$i]."</a>";
else
$text[$i] = $text[$i];
}
return implode(' ',$text);
}
I am newbie in optimization, so I want to ask: can I make this function work better/ faster? I heard that array_filter is better choice in this situation, however I can not make it work. Thanks for your help!
Now I have made one more function with array_map. so, which one is better to use?
function convert_links($text) {
$text = explode(' ', $text);
function convert_link($val) {
if(filter_var($val,FILTER_VALIDATE_URL))
return "<a href='".$val."' target='_blank'>".$val."</a>";
else
return $val;
}
$text = array_map('convert_link',$text);
return implode(' ',$text);
}
Danielius