Decision to avoid problem "it might replace a previously inserted value when doing multiple replacements. See also the examples in this document."
$urls - array of urls i want to replace with tag <a> and urls could be similar
http://abc.com/parameter/
http://abc.com/
// at first sort by length to have longest firstly
usort($urls,'sortByLen');
$replaces=[];
// replace all urls with unique
foreach($urls as $url){
$replace = '__REPLACE' . uniqid() . '__';
$text = str_replace($url,$replace, $text);
$replaces[$replace] = '<a href="' . $url . '">' . $url . '</a>';
}
foreach($replaces as $key => $replace){
$text = str_replace($key,$replace, $text);
}
--------------
function sortByLen($a,$b){
return strlen($b)-strlen($a);
}
Hope it will help others like me