• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

Converting Javascript code to PHP

I would like this javascript code converted to PHP. The example is shown here with an explanation of how it works:

http://scriptasylum.com/tutorials/encdec/encode-decode.html
0
ray-solomon
Asked:
ray-solomon
  • 6
  • 3
  • 2
2 Solutions
 
ThGCommented:

it is rather simple.. the following functions will perform both conversions, given $str as input:

function js_escape($str) {
  return rtrim("%" . chunk_split(bin2hex($str), 2, "%"), "%");
}

function js_unescape($str) {
  return rawurldecode($str);
}
0
 
ray-solomonAuthor Commented:
Here is the other part that i think finds the unicode values for each character in the string and shifts it. Then the function unshifts it again based on how many times it was shitfed to begin with.

<script language="javascript">
function dF(s){
var s1=unescape(s.substr(0,s.length-1)); var t='';
for(i=0;i<s1.length;i++)t+=String.fromCharCode(s1.charCodeAt(i)-s.substr(s.length-1,1));
document.write(unescape(t));
}
</script>


In other words, if I have an html file that includes a javascript file. And in the javascript file contains an encoded string in a variable and also the function to decode it. I need the php script to generate the encoded string to be written to the javascript file and a javascript function to decode the string in the javascript file.
0
 
ray-solomonAuthor Commented:
is there a way to convert that javascript function into php? The example is on the webpage about halfway down where it has two text boxes and a drop-down menu in the middle.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ray-solomonAuthor Commented:
Actually, this is the function I needed to be converted to PHP. I just need it to take a string and encode it:


var encN=1;
// ENCODES, IN UNICODE FORMAT, ALL TEXT AND THEN ESCAPES THE OUTPUT
function encodeTxt(s){
s=escape(s);
var ta=new Array();
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)+encN;
return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;
}
0
 
ThGCommented:
what this is about? i think i'm not understanding. this is a sort of rot13 function..but with offset 1.

if you put encN = 13, you get exactly str_rot13() built-in php function.

see http://it.php.net/manual/en/function.str-rot13.php
0
 
ray-solomonAuthor Commented:
I looked over the manual too. I see it look very similar.

Here is the explanation of the javascript:
   1. First, all the text is escaped.
(ThG already wrote this)

   2. Then the script finds the Unicode values for each character in the string.
(The escaped string is converted to unicode and stored in a variable)

   3. Then the script adds whatever the Code Key drop-down box value is to each character's Unicode value.
(var encN=1; This is the key code. Key code=1, otherwords, this is how many times the unicode characters get shifted forward)
The php function would convert every character in a string to its unicode equivelent, it would then shift each character forward however many times we want(a variable we determine, we will use 1 for this example).

   4. Then the script derives characters based on the shifted Unicode values.


   5. The Code Key value is also embedded in the decoded text so the script knows how to properly decode the string again.
(return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;) <- notice +encN at the end.

   6. Finally, it escapes the result one more time to remove any special characters. Now, the output looks totally foreign to someone who cannot un-shift Unicode values in their head. :)

<script type="text/javascript">
// I just modified this function to help you understand it
// ENCODES, IN UNICODE FORMAT, ALL TEXT AND THEN ESCAPES THE OUTPUT
var encN=1;
function encodeTxt(s){
s=escape(s);
var ta=new Array();
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)++encN;
return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;
}
var a=encodeTxt('HELLO')
alert (a);
</script>
(This will out put IFMMP) Which is "Hello" with every letter shifted forward once. (definded by encN=1;)

Ray
0
 
ray-solomonAuthor Commented:
oops change:
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)++encN;

to

for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)+encN;
0
 
SysTurnCommented:
<?php

$encN = 3;

function encodeTxt($s)
{
    // We need to globals the Enc Key to access it inside the function
    global $encN;
   
    // Uncomment to escape the string input
    // urlencode($s);
   
    $ta = '';
    for($i=0; $i<strlen($s); $i++)
    {
        $ta .= chr(ord(substr($s, $i, 1)) + $encN);
    }
   
    // Uncomment to escape the string output
    // urlencode($ta);
   
    return $ta;
}
echo encodeTxt('HELLO');

?>

Kind Regards
Bakr
0
 
SysTurnCommented:
P.S: I changed $encN to 3 instead of 1 just while testing :)
0
 
SysTurnCommented:
here are both encode and decode functions with urlencode "escape" enabled:

<?php

$encN = 1;

function encodeTxt($s)
{
    // We need to globals the Enc Key to access it inside the function
    global $encN;
   
    // Uncomment to escape the string input
    $s = urlencode($s);
   
    $ta = '';
    for($i=0; $i<strlen($s); $i++)
    {
        $ta .= chr(ord(substr($s, $i, 1)) + $encN);
    }
   
    // Uncomment to escape the string output
    $ta = urlencode($ta);
   
    return $ta;
}

function decodeTxt($s)
{
    // We need to globals the Enc Key to access it inside the function
    global $encN;

    // Uncomment to escape the string input
    $s = urldecode($s);

    $ta = '';
    for($i=0; $i<strlen($s); $i++)
    {
        $ta .= chr(ord(substr($s, $i, 1)) - $encN);
    }

    // Uncomment to escape the string output
    $ta = urldecode($ta);

    return $ta;
}

echo decodeTxt( encodeTxt('HEL & OO') );

?>
0
 
ray-solomonAuthor Commented:
This works exactly like its intended. Thank you so much everyone for contributing to this question. I could have not figured this out by myself anytime soon.
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.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now