<pre id="codeSnippet337997" class="prettyprint">$ta{$i
Main Topics
Browse All TopicsHello!
Please help me!
I am currently working on a small project. I have port a javascript code to PHP. Don't ask, I just have to. :)
The problem is that I can't get my PHP code work. There are some errors.
The code is an encrypting code.
**************************
The shortened html:
<form name="fa">
<textarea id="f2"></textarea>
select name="et" onchange="encN=this.select
<option selected>1
<option>2
<option>3
<option>4
<option>5
</select>
<input type="button" value="Encode" onclick="writeOut()">
**************************
The original JavaScript:
<script language="javascript">
var encN=1;
function encodeTxt(s){
s=escape(s);
var ta=new Array();
for(i=0;i<s.length;i++)ta[
return ""+escape(eval("String.fro
}
function writeOut(){
document.forms["fa"].outpu
}
</script>
NOTE: I have to get this **encodeTxt(document.forms
My PHP code is not working! I think I've done my best, I am really just a beginner. Please help!
The error msg is:
Notice: Undefined offset: 67 in \\.host\Shared Folders\htdocs\encoder\enc
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
2 ( I don't know what is this number 2.. Maybe the final string?)
Please help me to fix my code!
Maybe just a few fix, but I can't do more!
Thank you! You are my only hope!
If it's help, the original result of the Javascript (that I want) for "ssp_1-10_img (000).jpg" (without quotes) in the **encodeTxt(document.forms
ttq%602.21%60jnh%2631%2639
**************************
My PHP code (extracted to a snippet for readability and line numbering by mplungjan, ee zape):
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When testing, set $codeKey = 0; (NOT 1) when in JS: var codeKey=1;
or $encN = $codeKey+1; => $encN = $codeKey;
@mms_master:
Hey, thank you! Your code is really close to work perfect.
The only problem is this:
ttq%602.21%60jnh%2631
ttq%602.21%60jnh%2631%26
OK, I just checked it and the JS decoder both works with
ttq%602.21%60jnh%2631%
ttq%602.21%60jnh%2631%
Maybe because I use them to encode image URLs, and it displays them. So thank you!! You made my day! :)
I would be very happy if you could describe YOUR code in just 2-3 lines, because I would like to learn, not just copy&past :)
I know that rawurlencode() but I hadn't use it because I tested a string "abcde" and it gives back "abcde". It just escaping the special chars. While JS escape() makes "%61%62%63%64%65" from "abcde".
Thats why (and maybe because I'm a beginner) I dont understand that how can your code work. But it works!
So solution accepted, and thank you again! Just please help me to understand it :)
@storm_sw
Well spotted I missed that!
@Collis_Dam
First of all this is why you were getting errors:
Error:
"Notice: Undefined offset: 67 in \\.host\Shared Folders\htdocs\encoder\enc
Reason:
On line 28 you had "for ($j=1; $j<=$i; $j++) {". You were trying to use $i which had been declared in the previous 'for' loop but not this one. For more information on this try searching Google for "variable scope".
Fix:
I replaced $i with count($ta). Which as you probably know or guessed, returns the number of items in the array.
Error:
"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
Reason:
The eval() function evaluates PHP code. You were passing an invalid string to it. See www.php.net/eval for more information.
Fix:
Removed the eval function.
I then removed your escape function and used rawurlencode instead. But yes as you pointed out, javascritps escape function encodes all characters where as the php function only encode special characters. To replicate the javascript output you would need to write your own function as you tried to with escape. If you try this you would need to use ord() before bin2hex().
mms_master
Business Accounts
Answer for Membership
by: mms_masterPosted on 2009-06-05 at 04:42:15ID: 24555359
Hi,
I've fixed the errors and made some changes. It's not outputting the correct value yet. I'm at work at the moment and have to go do something. But maybe you can finish it. If not, I'll do some more work on it as soon as I get 5 minutes.
mms_master
Select allOpen in new window