I am trying to automatically format text to links in PHP but to trim long urls to a max character limit. And also remove 'http(s)' from outputted text.
blah blah http://example.com/some-long-slug-goes-here foo
should translate to:
blah blah <a href="http://example.com/some-long-slug-goes-here">example.com/some-long-sl...</a> foo
(blah blah example.com/some-long-sl... foo)
Found a preg_replace
solution here: How do I linkify urls in a string with php? but I'm incapable of editing it to my needs.
$string = preg_replace(
"~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
"<a href=\"\\0\">\\0</a>",
$string
);