Forum Moderators: coopster

Message Too Old, No Replies

How do you stop str ireplace ?

...i just need it to work once per word

         

londrum

7:39 pm on Dec 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hello. this is probably really easy, but i'm struggling with this little problem. i need to change every instance of the word 'blah' into a link <a href="example.html">blah</a>

so i'm storing the page contents into a buffer, and then using str_ireplace, like this...

$b = str_ireplace('blah','<a href="example.html">blah</a>', $b);

this works great... but it looks a little ugly when there are numerous instances of the word 'blah' on the page. i sometimes end up with as many as ten links all pointing to the same place.

so i was wondering if there was a way to stop str_ireplace after the first link has been changed.

i should mention that i've got actually got loads of words to change... blah2, blah3, blah4... etc etc. so i only need it to stop after it's done blah1, and then move onto blah2, blah3 etc.

i probably explained all that in a very confusing way... hope you can get the gist!

eelixduppy

7:51 pm on Dec 20, 2008 (gmt 0)



The str_replace functions replace all occurrences of a subject in a string, so you aren't going to be able to do what you want with that function. Preg_replace [php.net] however has a limit argument in which you can limit the number of replacements that are made--in your case 1. Take a look there and see what you can get working.

londrum

8:44 pm on Dec 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



cool that's fixed it, thanks