Solved

Converting Javascript code to PHP

Posted on 2006-07-14
11
438 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
Industry Leaders: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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