removing characters

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

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

removing characters

Post by Nay »

I use str_replace("/", "", $text) to remove the / in the $text.

But if $text is:

Code: Select all

/images/users/myavatar.jpg
then how can I only remove the first /. I tried:

Code: Select all

str_replace("/", "", $text, 1);
which didn't work.

Thanks,

-Nay
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Id use something like

Code: Select all

echo substr($text, 1);
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

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 »

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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

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