views:

269

answers:

5

Example
Input = 1.1.0.1
Expected output = 1.101

+7  A: 

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;
zombat
A: 

I though substr_replace() would work here, but sadly no... Here is a regex approach:

$str = preg_replace('~(\d+\.)(\d+)\.(\d+)\.(\d+)~', '$1$2$3$4', $str);
Alix Axel
+2  A: 
$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] ) );
ghostdog74
or `array_unshift( $s ) ;`
St.Woland
A: 
$count = 0;
$output = $input;
do {
    $output = preg_replace('/^(.+\.)(.+)\./', '$1$2', $output, -1, $count);
} while ($count != 0);
echo $output;
Greg Bacon
+5  A: 
$s = preg_replace('/((?<=\.)[^.]*)\./', '$1', $s);
Alan Moore
the only correct answer so far ;)
stereofrog