3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $inputString = "8"; $pattern = '/[ \-]/'; $replacement = ''; $inputString = preg_replace($pattern,$replacement,$inputString); $inputString = trim($inputString); $count = strlen($inputString); //echo $inputString;exit; $rem = ($count%3); if ($rem == 1){ $pos =4; $count = ($count - $pos); } else if($rem == 2) { $pos = 2; $count = ($count - $pos); } $phoneNumber = ""; for($k=0;$k<$count;$k++){ if($k % 3 !=0 || $k == 0){ $phoneNumber = $phoneNumber.$inputString[$k]; $phoneNumber = trim($phoneNumber); }else{ $phoneNumber = $phoneNumber.'-'.$inputString[$k]; } } //echo $phoneNumber;exit; if($rem == 2){ if($count!=0){ $phoneNumber.= '-'; } $phoneNumber.= $inputString[$count].$inputString[$count+1]; }else if($rem == 1){ if($count!=0){ $phoneNumber.='-'; } $phoneNumber.= $inputString[$count].$inputString[$count+1].'-'.$inputString[$count+2].$inputString[$count+3]; } echo $phoneNumber;exit;
based on uSUEX
Output for 7.1.0 - 7.3.0rc4
Notice: Uninitialized string offset: -3 in /in/H5u1q on line 43 Notice: Uninitialized string offset: -2 in /in/H5u1q on line 43 --88
Output for 5.3.20 - 7.0.20
Notice: Uninitialized string offset: -3 in /in/H5u1q on line 43 Notice: Uninitialized string offset: -2 in /in/H5u1q on line 43 Notice: Uninitialized string offset: -1 in /in/H5u1q on line 43 --8