Page 1 of 1

removing characters

Posted: Wed Sep 17, 2003 5:57 am
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

Posted: Wed Sep 17, 2003 6:07 am
by JAM
Id use something like

Code: Select all

echo substr($text, 1);

Posted: Wed Sep 17, 2003 8:59 am
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

Posted: Wed Sep 17, 2003 3:25 pm
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

Posted: Wed Sep 17, 2003 6:26 pm
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?