Search code examples
phpregexdetection

To be able to find more than one link


Code can only find and change one image link in the text. If it's more than one, it doesn't work. How can I get it to detect multiple image links?

$sad222 = "somthing text bla bla bla ...... https://www.indyturk.com/sites/default/files/styles/150x100/public/article/main_image/2022/08/02/984246-1125154792.jpg asdas https://www.indyturk.com/sites/default/files/styles/800x600/public/article/main_image/2022/11/18/1055251-759331593.jpg  121das";
    function findAndChangeImgLinksInStrings($string)
    { 
        $reg_exUrl = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/m'; 
        if (preg_match_all($reg_exUrl, $string, $urls, PREG_SET_ORDER, 0)) {
            foreach ($urls as $url) { 
                $newLinks = $url[0][0];
                if (strstr($newLinks, ":") === false) {
                    $link = 'https://' . $newLinks;
                } else {
                    $link = $newLinks;
                }
                $exploded = explode($link, $string);
                $string_before = $exploded[0];
                $string_after = $exploded[1];
                if (strtolower(substr($link, 0, 7)) == "http://" || strtolower(substr($link, 0, 7)) == "ftps://" || strtolower(substr($link, 0, 8)) == "https://" || strtolower(substr($link, 0, 6)) == "ftp://") {
                    if (strtolower(substr($link, strlen($link) - 4, 4)) == ".jpg" || strtolower(substr($link, strlen($link) - 4, 4)) == ".jpe" || strtolower(substr($link, strlen($link) - 4, 4)) == ".jif" || strtolower(substr($link, strlen($link) - 4, 4)) == ".jfi" || strtolower(substr($link, strlen($link) - 4, 4)) == ".gif" || strtolower(substr($link, strlen($link) - 4, 4)) == ".png" || strtolower(substr($link, strlen($link) - 4, 4)) == ".bmp" || strtolower(substr($link, strlen($link) - 4, 4)) == ".dib" || strtolower(substr($link, strlen($link) - 4, 4)) == ".ico" || strtolower(substr($link, strlen($link) - 5, 5)) == ".jpeg" || strtolower(substr($link, strlen($link) - 5, 5)) == ".jfif" || strtolower(substr($link, strlen($link) - 5, 5)) == ".apng" || strtolower(substr($link, strlen($link) - 5, 5)) == ".tiff" || strtolower(substr($link, strlen($link) - 4, 4)) == ".tif") {
                        $imageCode = erisimKoduOlustur();
                        getFile($link, '/images/' . $imageCode . strtolower(substr($link, strlen($link) - 4, 4)));
                    }
                }
                
                return $string_before . '<a class="noteImageInQNote" href="https://example.com/images/' . $imageCode . strtolower(substr($link, strlen($link) - 4, 4)) . '" target="_blank"><img class="noteImageInQNote" src="https://example.com/images/' . $imageCode . strtolower(substr($link, strlen($link) - 4, 4)) . '"></a>' . $string_after;
            }
        }
        
        return $string;
    }
    
    echo findAndChangeImgLinksInStrings($sad222);

Solution

  • You can clean up your code and search in a much simpler way using PHP native functions.

    First of all the pattern: /(https?|ftps?)\:\/\/[^\s]*/ which will find http and https as well as ftp and ftps. With that you have all matches in one array.

    Using pathinfo() you can extract the extension and check if it is in an array of wanted extensions.

    $sad222        = "somthing text bla bla bla ...... https://www.indyturk.com/sites/default/files/styles/150x100/public/article/main_image/2022/08/02/984246-1125154792.jpg asdas https://www.indyturk.com/sites/default/files/styles/800x600/public/article/main_image/2022/11/18/1055251-759331593.jpg  121das";
    
    $findImageUrls = function (string $string): array {
        $images = [];
        preg_match_all('/(https?|ftps?)\:\/\/[^\s]*/m', $string, $urls);
        foreach ($urls[0] as $url) {
            if (!in_array(strtolower(pathinfo($url, PATHINFO_EXTENSION)),
                    ['jpg', 'jpe', 'jpeg', 'jif', 'jfif', 'jfi', 'png', 'gif', 'bmp', 'dib', 'ico', 'apng', 'tiff', 'tif'])
            ) continue;
            $images[] = $url;
        }
    
        return $images;
    };
    
    print_r($findImageUrls($sad222));
    

    Output

    Array
    (
        [0] => https://www.indyturk.com/sites/default/files/styles/150x100/public/article/main_image/2022/08/02/984246-1125154792.jpg
        [1] => https://www.indyturk.com/sites/default/files/styles/800x600/public/article/main_image/2022/11/18/1055251-759331593.jpg
    )
    

    Update

    Based on your comment you want to manipulate the URLs. Here's an example on you may do this.

    $imageUrlsToHtml = function (array $images, string $text): string {
        foreach (array_unique($images) as $image) {
            $text = str_replace($image, "<a href='$image' class='noteImageInQNote' target='_blank'><img src='$image' alt='image'></a>", $text);
        }
    
        return $text;
    };
    
    echo $imageUrlsToHtml($findImageUrls($sad222), $sad222);
    

    Output

    somthing text bla bla bla ...... <a href='https://www.indyturk.com/sites/default/files/styles/150x100/public/article/main_image/2022/08/02/984246-1125154792.jpg' class='noteImageInQNote' target='_blank'><img src='https://www.indyturk.com/sites/default/files/styles/150x100/public/article/main_image/2022/08/02/984246-1125154792.jpg' alt='image'></a> asdas <a href='https://www.indyturk.com/sites/default/files/styles/800x600/public/article/main_image/2022/11/18/1055251-759331593.jpg' class='noteImageInQNote' target='_blank'><img src='https://www.indyturk.com/sites/default/files/styles/800x600/public/article/main_image/2022/11/18/1055251-759331593.jpg' alt='image'></a> 121das