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);
2
Answers
This function will split your string up by spaces then if they’re web links that include .jpg put them into an array. Tested with your example string.
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.
Output
Update
Based on your comment you want to manipulate the URLs. Here’s an example on you may do this.
Output