PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Wed Sep 17, 2003 5:57 am
I use str_replace("/", "", $text) to remove the / in the $text.
But if $text is:
then how can I only remove the first /. I tried:
which didn't work.
Thanks,
-Nay
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Wed Sep 17, 2003 6:07 am
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Wed Sep 17, 2003 8:59 am
oh, but how about IF I wanted to get the result when you search the second time, then replace it with something else, or is that not possible?
-Nay
m3rajk
DevNet Resident
Posts: 1191 Joined: Mon Jun 02, 2003 3:37 pm
Post
by m3rajk » Wed Sep 17, 2003 3:25 pm
use preg instead.
Code: Select all
$text=preg_replace('|/|','',$text,1);or
Code: Select all
$text=preg_replace('|\b/\w+\b|','',$text);the first replaces the first occurance of / in the string
the second replaces any occurance / that starts a "word" string
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Wed Sep 17, 2003 6:26 pm
Nay wrote: oh, but how about IF I wanted to get the result when you search the second time, then replace it with something else, or is that not possible?
Either m3rajk's example, using ereg_replace() or
Code: Select all
echo str_replace(substr($text, 1),"whatever",$text);
Or did I misunderstand?