integer to string convertion function

hey, i need to convert integer to string
like
"1745" to "agde"

and then to int again...

 "agde"  to "1745"...
svsvsvAsked:
Who is Participating?
 
Harisha M GConnect With a Mentor Commented:
Hi,

<?php

function int2str($x)
{
    $x = sprintf("%s",$x);
    for($i=0;$i<strlen($x);$i++)
    {
        $y .= chr($x[$i]+96);
    }
    return $y;
}

function str2int($x)
{
    $y = "";
    for($i=0;$i<strlen($x);$i++)
    {
        $y .= sprintf("%d",ord($x[$i])-96);
    }
    return $y;
}

echo int2str("1732");
echo str2int("abcd");

?>

---
Harish
0
 
RoonaanCommented:
Hi,

You can use this function:

<?php
 function swap($in) {
    $tokens = "abcdefghij1234567890";
    $out = '';
    for($i = 0, $c = strlen($in); $i < $c; $i++) {
      $out .= substr($tokens, (strpos($tokens, substr($in,0,1)) + 10) % 20, 1);
      $in = substr($in,1);
    }
    return $out;
 }
 echo swap('1745').'<br/>';
 echo swap('agde');
?>

It does not take into account characters above "j" or non-alphanumeric characters.

-r-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.