I have seen a lot of topics related to this but can't find something that's working for links AND images.
On my PHP page I echo $content, which contains a record from my DB. In this string, there can be urls and image urls. What I need is a function that automatically finds these urls and displays them in proper HTML. So normal links should be shown as <a ...>....</a>
and image links (ending with jpeg, jpg, png, gif,...) should be shown as <img ...>
.
This is what I found for the url website links only:
$content = preg_replace("~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
"<a href=\"\\0\">\\0</a>",
$content);
echo $content;
I think I should use some regex code for this but I'm not very familiar with that.Thanks a lot!
EDIT:
http://example.com, https://example.com should all be shown like <a href="url">url</a>
. All urls that are not images;
http://www.example.com/image.png should be shown as <img src="http://www.example.com/image.png">
This goes for all urls ending with an image extension like png, jpeg, gif etc