Page 1 of 1

Finding a character, but skipping the first found character

Posted: Thu Oct 26, 2006 2:22 am
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

Posted: Thu Oct 26, 2006 2:31 am
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

Posted: Thu Oct 26, 2006 2:43 am
by tarja311
That will work. Thank You.