Mattigrate Posted November 2, 2010 Share Posted November 2, 2010 Here's my code, a simple replace $newlink = str_replace("/http:\/\/images\.4chan\.org\/(.+)\/src\//", "thread/", "http://images.4chan.org/adv/src/1288558759794.png"); Print $newlink; I want it to output "thread/1288558759794.png", but its just giving me the link I started with. Any ideas whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/ Share on other sites More sharing options...
trq Posted November 2, 2010 Share Posted November 2, 2010 str_replace doesn't work with regular expressions. Take a look at preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/#findComment-1129296 Share on other sites More sharing options...
Mattigrate Posted November 2, 2010 Author Share Posted November 2, 2010 Ok, preg_replace works if I only want to replace one instance, but my string contains multiple links. I want to turn this $html = "http://images.4chan.org/adv/src/1288558759794.png blah blah http://images.4chan.org/sci/src/12888989591212.png blah" into this $html = "thread/1288558759794.png blah blah thread/12888989591212.png blah" How can I get it to replace all of them? Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/#findComment-1129611 Share on other sites More sharing options...
AbraCadaver Posted November 2, 2010 Share Posted November 2, 2010 Works for me, so I assume that your links are not on the same line? the . dot doesn't match newline. Either use the s modifier or change the pattern: /http:\/\/images\.4chan\.org\/(.+)\/src\//s or /http:\/\/images\.4chan\.org\/[^\/]+\/src\// And to be more readable use different delimiters: ~http://images\.4chan\.org/[^/]+/src/~ Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/#findComment-1129613 Share on other sites More sharing options...
Mattigrate Posted November 2, 2010 Author Share Posted November 2, 2010 Still not working for me. $oldhtml = "http://images.4chan.org/adv/src/1288558759794.png blah blah http://images.4chan.org/sci/src/12888989591212.png blah"; $html = preg_replace("/http:\/\/images\.4chan\.org\/(.+)\/src\//s", "thread/", $oldhtml); echo $html; only outputs "thread/12888989591212.png blah" Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/#findComment-1129615 Share on other sites More sharing options...
akitchin Posted November 2, 2010 Share Posted November 2, 2010 your issue is greediness. patterns are by default greedy in the PCRE functions in PHP. what this means is they will match the LARGEST POSSIBLE pattern. since you have (.+) in your pattern, that means that this whole thing: "http://images.4chan.org/adv/src/1288558759794.png blah blah http://images.4chan.org/sci/src/" will be matched, since "adv/src/1288558759794.png blah blah http://images.4chan.org/sci" matches the (.+) subpattern you have in there. use the question mark after your quantifier to make it ungreedy by default: "#http:\/\/images\.4chan\.org\/(.+?)\/src\/#s" otherwise you can use the U modifier at the end of the pattern: "#http:\/\/images\.4chan\.org\/(.+)\/src\/#sU" EDIT: AbraCadaver's post will also work. Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/#findComment-1129620 Share on other sites More sharing options...
AbraCadaver Posted November 2, 2010 Share Posted November 2, 2010 Still not working for me. $oldhtml = "http://images.4chan.org/adv/src/1288558759794.png blah blah http://images.4chan.org/sci/src/12888989591212.png blah"; $html = preg_replace("/http:\/\/images\.4chan\.org\/(.+)\/src\//s", "thread/", $oldhtml); echo $html; only outputs "thread/12888989591212.png blah" My bad, use the other pattern: $html = preg_replace("~http://images\.4chan\.org/[^/]+/src/~", "thread/", $oldhtml); Quote Link to comment https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/#findComment-1129621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.