I have found alot of functions that answers my question in the top, but most of theese functions have problems. The best what I have so far found is this one
//Replace links with http://
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" class=\"chatlink\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret);
//Replace links without http://
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" class=\"chatlink\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret);
The problem is, this doesn't replace the url, if it is in the middle of the text. For example, it does not replace text in string something like this:
"Small text http://www.google.com , and some more text." It works fine if the link is not in the the middle of the text, but in new line.
And secondly I need to make sure, if in string already is html href link, that it doesn't replace the url again.
<a href="http://google.com">http://google.com</a>
<a href="<a href="http://google.com">http://google.com</a>"></a>
That would be it. Would be amazing if someone could help me, Im just not too good with preg_replace function.