Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Created September 25, 2017 08:43
Show Gist options
  • Save HoangPV/292cc8d76a4ba9f3a8d7d0ccd304e710 to your computer and use it in GitHub Desktop.
Save HoangPV/292cc8d76a4ba9f3a8d7d0ccd304e710 to your computer and use it in GitHub Desktop.
Dashatize it
<?php
/**
* Dashatize it
*
* @author Phan Vu Hoang <[email protected]>
*/
require '../vendor/autoload.php';
function dashatize( $num ) {
$str = (string)($num);
$str = str_replace('-', '', $str);
$len = strlen($str);
$ret = [];
for ($i=0; $i < $len; $i++) {
# code...
echo $str[$i] . ' ';
if ( (int)$str[$i] % 2 == 0) {
$ret[] = $str[$i];
} else {
$ret[] = '-' .$str[$i] . '-';
}
}
$ret = implode('', $ret);
if($ret[0]=='-') $ret = substr($ret, 1);
if(substr($ret, -1)=='-') $ret = substr($ret, 0, -1);
$ret = str_replace('--', '-', $ret);
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment