Replace all certain character except first occurrence in PHP Replace all certain character except first occurrence in PHP php php

Replace all certain character except first occurrence in PHP


You could make use substr() and str_replace() fairly easily:

$str = '1.1.0.1';$pos = strpos($str,'.');if ($pos !== false) {    $str = substr($str,0,$pos+1) . str_replace('.','',substr($str,$pos+1));}echo $str;


$s = preg_replace('/((?<=\.)[^.]*)\./', '$1', $s);

Matches zero or more non-dot characters followed by a dot, but only if the match was preceded by a dot. This prevents a match on the initial digit(s). Replaces the match with only the non-dot characters (the digits), which were captured in group #1.


$input="1.1.1.1";$s = explode(".",$input ) ;$t=array_slice($s, 1);print implode(".",array($s[0] , implode("",$t)) );

or

$input="1.1.1.1";$s = explode(".",$input ,2) ;$s[1]=str_replace(".","",$s[1]);print implode(".",array($s[0] ,$s[1] ) );