Jump to content

Why isn't this str_replace working?


Mattigrate

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/217525-why-isnt-this-str_replace-working/
Share on other sites

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?

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/~

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"

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.

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.