Jump to content

Why isn't this str_replace working?


Mattigrate
 Share

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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.