Re: replacing first occurence

From: Date: Wed, 24 Apr 2002 19:11:31 +0000
Subject: Re: replacing first occurence
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-94436@lists.php.net to get a copy of this message
> What function can I use to replace just the first occurence > of a "," in a string? Let's replace the first , with a ! and if , does not exist at all let's not replace anything. Change according to taste: $str = 'Hello, I love you!'; if (($pos = strpos($str, ',')) !== false) { $str{$pos} = '!'; } print $str; // Hello! I love you!'; print $pos; // 5 print $str{0}; // H print $str{5}; // ! Regards, Philip Olson

« previous php.general (#94436) next »