Part 3 of the script I'm slowly writing up 🙂
This converts all http://www.whatever.com/ to a link. It cuts off things at 60 characters.
$pattern = '/([^"\'])(http[s]?:\/\/[[a-zA-Z0-9_-]+\.]?[a-zA-Z0-9_\/?&%-]+\.[a-zA-Z0-9_-]+\/?[[a-zA-Z0-9+_:;=\/?&%\.-]+]?)/e';
$text = preg_replace($pattern, "'\\1<a href=\"\\2\">'.substr('\\2', 0, 60).'</a>'" , $text);
I tried doing some type of if statement like
$replacement = "'\\1<a href=\"\\2\">'.(($blah) ? substr(\"\\2\", 0, 5) : \"No blah!\").'</a>'"
But it just blanks out $text. I need to make it so things over 60 characters get a ... on the end, otherwise don't truncate. I know to use strlen instead of $blah after but I can't get this simple test to work.