Created
February 2, 2018 15:51
-
-
Save vaseemansari007/47149238d2266be2ccf7b838368ff07a to your computer and use it in GitHub Desktop.
Basic Program to output n digits from a given string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function encode($string, $outputLength=6) { | |
// echo '<br>'.$string; | |
$string = md5($string); | |
//echo '<br>'.$string; | |
$ans = array(); | |
$string = str_split($string); | |
for ($i = 0; $i < count($string); $i++) { | |
if((int)($string[$i])){ | |
$ans[] = $string[$i]; | |
} | |
} | |
$ans = array_unique($ans); | |
echo '<pre>';print_r($ans); | |
$outputString = implode('', $ans); | |
if(strlen($outputString) >= $outputLength){ | |
$outputString = substr($outputString,0,$outputLength); | |
}else{ | |
$howMuchLess = $outputLength - strlen($outputString); | |
//$outputString = $outputString.str_repeat("0", $howMuchLess); | |
$outputString = $outputString.implode('', array_slice($ans, 0, $howMuchLess)); | |
} | |
return $outputString; | |
} | |
$sku = 'sr0112s'; | |
$original = strtolower($sku); | |
$encoded = encode($original); | |
echo $encoded . "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment