Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pushing the str_replace function limits

Status
Not open for further replies.

minsan

Technical User
Jan 8, 2001
18
US
Is there a way to customize the str_replace function so that instead of replacing ALL occurences of the string needle, it will only replace the first occurence? or maybe the last occurence? or the n th occurence?

Any ideas would be appreciated.

minsan
 
Well, there are functions to find the first and last occurances of a string.

They are strchr() and strrchr(). The first one finds the first occurance of a character in a string, and the second finds the last occurange of a character in a string.

There are no functions to find the nth ocurrance of the string, so you would have to write one.

Hope this helps.
-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Vic

It looks like strchr() and strrchr() will only find the needle. What I really need is a function that will replace the needle.

As far as replacing the nth occurence, has anybody out there written a custom function like this before? I've searched the forum archives with no luck.

minsan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top