Solved

Converting Javascript code to PHP

Posted on 2006-07-14
11
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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