String to URL but detect if url is image?


i’m trying to do something in PHP

I’m trying to get the link of an image -> store it to my DB, but I’d like the user to be able to store text before it, and after it, I’ve gotten my hands on a similar function for links, but the image part is missing.

As you can see the turnUrlIntoHyperlink does a regex check over the entire arg passed, turning the text that contains it to the url, so users can post something like
Hey check this cool site "" its dope!
And the entire argument posting to my database.

However i can’t seem to get the same function working for the Convert Image, as it simply won’t post and removed text before/after it before when i made the attempt.

How would i do this in a correct way, and can i combine these 2 functions in to 1 function?

function convertImg($string) {
    return preg_replace('/((https?)://(S*).(jpg|gif|png)(?(S*))?(?=s|$|pP))/i', '<img src="$1" />', $string);

function turnUrlIntoHyperlink($string){
    //The Regular Expression filter
    $reg_exUrl = "/(?i)b((?:https?://|wwwd{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^s()<>]+|(([^s()<>]+|(([^s()<>]+)))*))+(?:(([^s()<>]+|(([^s()<>]+)))*)|[^s`!()[]{};:'".,<>?«»“”‘’]))/";
    // Check if there is a url in the text
    if(preg_match_all($reg_exUrl, $string, $url)) {

        // Loop through all matches
        foreach($url[0] as $newLinks){
            if(strstr( $newLinks, ":" ) === false){
                $link = 'http://'.$newLinks;
                $link = $newLinks;

            // Create Search and Replace strings
            $search  = $newLinks;
            $replace = '<a href="'.$link.'" title="'.$newLinks.'" target="_blank">'.$link.'</a>';
            $string = str_replace($search, $replace, $string);

    //Return result
    return $string;

Source: Ask PHP