0

I have a very complex string and i want to find and replace all urls that end with .jpg, .png, or .gif with a static url. For example;

$string = "Lorem ipsum dolor sit amet, http://www.website.com/images/some.jpg; consul delicata comprehensam eos at. Mea rebum laudem deterruisset et, ex epicuri constituto his. Sea ad appareat democritum. http://www.anotherwebsite.com/images/some.png Omnis vituperata dissentiunt an duo. Sumo diceret lobortis at sed, singulis aliquando prodesset ex sit.";

$regex = ?;
$static_image_url= "http://new_image_url";

return preg_replace($regex, $static_image_url, $string);

Please note this string may be more complex than this, urls are dynamic and this is NOT HTML.

Thank you very much in advance.

Best Regards,

EDIT:

This one worked for this case;

return preg_replace('/https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif)/', $static_image_url, $string);
3
  • Yeah, so what have you tried? Commented Feb 11, 2014 at 22:01
  • I'm middle of nowhere! studied and tried to edit this one but no luck stackoverflow.com/questions/7461922/… Commented Feb 11, 2014 at 22:04
  • Try something like this to start with: regex101.com/r/nX1sN3 Commented Feb 11, 2014 at 22:11

2 Answers 2

1

This should replace the links as long as they don't have a space in them (since links clearly can't have spaces).

$string = "Lorem ipsum dolor sit amet, http://www.website.com/images/some.jpg; consul delicata comprehensam eos at. Mea rebum laudem deterruisset et, ex epicuri constituto his. Sea ad appareat democritum. http://www.anotherwebsite.com/images/some.png Omnis vituperata dissentiunt an duo. Sumo diceret lobortis at sed, singulis aliquando prodesset ex sit.";
$static_image_url= "http://new_image_url";
return preg_replace('/https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif)/', $static_image_url, $string);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I've found a very smilar one that worked. But yours seems much detailed.
The problem with yours is that it does not do what it should. For example, it replaces htt://test.jpg and http://www.the link.jpg and you're welcome.
Yes, yours definitely better!
Thanks. Good luck with your program. Also, if you want to allow spaces in links, you can just replace the [^ ] with ..
0

For figuring out a regex, use the online tools for testing out your string.

http://www.regextester.com.

Since in your example string all your links start with "http://" we can start our pattern with that. We know that they are going to end in the extension of the file type (jpg, png) and a whole mess of stuff in between.

http:\/\/.+?(\.jpg|\.png|\.gif)

Using .+ will match anything and appending ? makes it ungreedy. Otherwise, we will get everything from the beginning of the first link to the end of the last.

Warning This regex is based on your example string. If the links are 'www.someplace.com/someimage.jpg' it won't be matched

1 Comment

This would incorrectly replace links such as http://www.website.com/images/somejpg. Also, you forgot .gif.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.