Reputation: 589
I would like to change number (ie. 19455) into something shorter using numbers and digits (ie. w3b) so for example number 19455 is replaced by w3b. I would like to be able to get back that number when I enter w3b. Is there a ready function that I could use or do I have to build it by myself?
Upvotes: 1
Views: 246
Reputation: 2679
I hope this will do...
<?
# MAKE STRING INTO ARRAY TO FOR NUMBERS 0-9
$numbers = array();
for($counter =0; $counter <= 10; $counter++) {
$numbers[$counter] = $counter;
}
# FUNCTION TO CHANGE NUMBER INTO LETTERS
function change_to_letters($string) {
global $numbers;
$replacements = array("a","b","c","d","e","f","g","h","i","j");
$string = str_replace($numbers, $replacements, $string);
return $string;
}
if(isset($_POST['submit'])){
echo 'I changed '.$_POST['string'].' to this:<div style="border: 1px solid #666;background- color: #E6FAD9;width:200px;padding:5px;">'.change_to_letters($_POST['string']).'</div>';
}
?>
<!-- INPUT FORM --->
<form action="" method="post">
Enter some numbers here and i will change them into letters:<br /><input name="string" type="text" size="15" maxlength="10"><br />
<input type="submit" name="submit" value="Convert"><br />
</form>
Upvotes: 0
Reputation: 9523
Use base_convert
php function:
Example:
$myNumInDecimal = "1234567787";
$myNumInBase36 = base_convert($myNumInDecimal, 10, 36);
this will give you:
kf12ln
http://php.net/manual/en/function.base-convert.php
BUT notice that both to and from bases should be between 2..36.
Upvotes: 4
Reputation:
Try to a function that convert your number which are in decimal to hexadecimal , that will make a good solution I guess
Upvotes: 0