<?php
$text = "labas tralala asda code:8745 sfd code:9454563 asda";
$pattern = '#code:(.*?) #si';
preg_match_all($pattern, $text, $matches);
foreach ($matches[1] as &$id) {
// select * from lenta where id=$id
$text = str_replace("code:".$id,"<a href='http://labas(".$id.")'>Nuoroda(".$id.")</a>",$text);
}
echo $text;
?>