function line_encoding($e){
$s = preg_split("//",$e);
$s[-1] = "";
$o = 1;
$f = "";
for($c=0;$c<strlen($e);$c++){
if($s[$c]==$s[$c-1]){
$o++;
}else{
$f .= $o==1?$s[$c-1]:($o.$s[$c-1]);
$o=1;
}
}
return $f;
}
Check if this works
7
solved Compressing a string PHP [closed]