Solved

Converting Javascript code to PHP

Posted on 2006-07-14
11
437 Views
Last Modified: 2008-03-17
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
Comment
Question by:ray-solomon
  • 6
  • 3
  • 2
11 Comments
 
LVL 14

Assisted Solution

by:ThG
ThG earned 100 total points
ID: 17111695

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
 
LVL 10

Author Comment

by:ray-solomon
ID: 17112168
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
 
LVL 10

Author Comment

by:ray-solomon
ID: 17119681
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Author Comment

by:ray-solomon
ID: 17132828
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
 
LVL 14

Expert Comment

by:ThG
ID: 17134617
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
 
LVL 10

Author Comment

by:ray-solomon
ID: 17135054
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
 
LVL 10

Author Comment

by:ray-solomon
ID: 17135085
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
 
LVL 6

Expert Comment

by:SysTurn
ID: 17139204
<?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
 
LVL 6

Expert Comment

by:SysTurn
ID: 17139212
P.S: I changed $encN to 3 instead of 1 just while testing :)
0
 
LVL 6

Accepted Solution

by:
SysTurn earned 400 total points
ID: 17139285
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
 
LVL 10

Author Comment

by:ray-solomon
ID: 17140389
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question