Finding a character, but skipping the first found character

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
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Finding a character, but skipping the first found character

Post by tarja311 »

Hi all,

I have a string and it contains more than one '-' character. I am trying to figure out a way to skip the first occurance of this character but change the rest of them. Anyone have any idea how to do this?

Thanks

-- tarja
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

If i understood correctly, you want to replace all "-" characters except first, then

Code: Select all

function my_replace($search, $replacement, $string)
{
    $pos = strpos($string, $search);
    if ($pos === false) return $string;
    $pos++;
    return substr($string,0,$pos).str_replace($search, $replacement, substr($string, $pos));
}
Haven't tested, but hope it works
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

That will work. Thank You.
Post Reply