473,388 Members | 868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Convert Perl Code in PHP

Hi Every one,

I have a perl script which uses the following code to decode the username saved in a cookie. I want to parse that cookie in php for adding some more features in the application. The source application in perl but I don't know any thing about perl. Here is the code ...

Expand|Select|Wrap|Line Numbers
  1. @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
  2. for ($_ = 0; $_ <= $#table; $_++) {
  3. $decode_table[ord($table[$_])] = $_;
  4. }
  5. local $_ = $_[0];
  6. s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
  7. pack('B' . (int(length($_) / 8) * 8), $_);
Can any one help me to convert this code in PHP?

Thanks in advance
Jun 24 '08 #1
9 4963
3,509 Expert Mod 2GB
Sure, if this were the PHP forum . Unfortunately, it is not and I know that I do not know any PHP. What I can do though, is move this to the PHP forum where hopefully they can help you.

Please know that you might have to specify what your requirements are for the script to do and also write code (these are learning forums).

On the other hand, if you are looking for someone to write this for you, then you can certainly post this to the Jobs forum and offer to pay someone. The choice is up to you.


Jun 30 '08 #2
5,821 Expert 4TB
Heya, aliusman.

I'm no Perl guru, but I dabble from time to time in the dark arts. Let's see if we can run through this and generate a suitable translation.

And if I happen to get anything wrong, perhaps numberwhun would be wiling to step in and assist... *AHEM*.


Expand|Select|Wrap|Line Numbers
  1. @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
This line creates a big 'ol table with all the letters and numbers, and a few symbols to boot.

Expand|Select|Wrap|Line Numbers
  1. for ($_ = 0; $_ <= $#table; $_++) {
  2. $decode_table[ord($table[$_])] = $_;
  3. }
Now we're looping through the table array that we created and generating a new hash called #decode_table (in Perl, hashes start with the '#' character, but you reference individual elements in the hash using '$'). The keys of this table are the ordinal values of @table and the values start with 0 and increase by 1 each time.

Expand|Select|Wrap|Line Numbers
  1. local $_ = $_[0];
This creates a local variable $_ and sets it equal to the first index in the special $_ variable (rough equivalent in PHP is probably $_GET or $_COOKIE).

Expand|Select|Wrap|Line Numbers
  1. s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
  2. pack('B' . (int(length($_) / 8) * 8), $_);
Got no idea here. Perhaps numberwhun could be of some assistance as to the purpose of these lines. *AHEM*
Jul 2 '08 #3
6,050 Expert 4TB

Sounds like you need some cough medicine, Josh.
Jul 2 '08 #4
5,821 Expert 4TB
Ooh, cherry-flavored.

Alrightey. After further consultation with our Perl forum leader, we (well, he) determined that the code you provided is not adequate to determine what's going on.

He checked it out, and here are the errors it generates:

Expand|Select|Wrap|Line Numbers
  1. Useless use of pack in void context at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 15.
  2. Use of uninitialized value $_ in substitution (s///) at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 13.
  3. Use of uninitialized value $_ in length at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 15.
  4. Use of uninitialized value $_ in pack at C:/coding/Eclipse_Workspace/Perl_Testing_Area/test4.pl line 15.
If you can, please post the rest of the code. If that's all you have, we'll need some more info on where the code came from.
Jul 3 '08 #5
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
  2. pack('B' . (int(length($_) / 8) * 8), $_);
Got no idea here. Perhaps numberwhun could be of some assistance as to the purpose of these lines. *AHEM*
That's probably intended to be the encoding/decoding part.
The pack converts a string into some machine level representation and the unpack does the opposite. The OP might just want to explain what their algorithm is for the decode and then we could suggest the php equivalence of that.
Jul 3 '08 #6
5,821 Expert 4TB
Unfortunately, PHP's pack() function takes a slightly different syntax than Perl's (http://php.net/pack), so I'm afraid that this is beyond my ken.

I will go ahead and move this thread back to the Perl forum so that the OP might be able to get a better idea of what's going on. Once we have that, then we can tackle how to translate it.
Jul 4 '08 #7
4,059 Expert 2GB
it would be nice to see what the value of $_ is from this line of the code:

local $_ = $_[0];

without knowing what the input is trying to figure out the correct output is going to be useless. But I have a feeling this thread has been abandoned anyway.
Jul 4 '08 #8
$decoded_string = base64_decode($thestring);

echo $decoded_string;

Mar 7 '09 #9
3,509 Expert Mod 2GB
This thread has not been posted to for about 8 months. It is quite possible that the OP has either forgotten about this issue, solved i t or moved on.

While it is commendable to provide answers, please keep these things in mind in the future.


Mar 8 '09 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
by: steeve.nadeau | last post by:
Hi I search algorithme to convert XY into Latitude/longitude to use customer data with GPS... thk in advance.
by: Manzoorul Hassan | last post by:
I need to convert a date / time to NTP Timestamp, in HEX no less, and was wondering if there might be a module for it. Any ideas? I have been searching for NTP Timestamp under Perl but have not...
by: gilad | last post by:
Hi, I have just released the initial beta of a C# project called 'aumplib'. aumplib is a C# namespace which is made up of a set of classes that interface several prominent open source audio...
by: =?ISO-8859-7?B?1/H189zt6Ocgwfrt4eve?= | last post by:
How can I convert a perl script to Python? Thank you!
by: parajaganesh | last post by:
I am a new man to Perl. Anybody can help me to convert a perl program into .com format.
by: poolboi | last post by:
hi guys, i've read a lot of thread of converting excel data into CSV files. however, i need a perl script to convert CSV file into excel file now, if required modules is needed, it would be...
by: pointzero | last post by:
Can someone help me to convert the following perl code into php? Leave the Net::DNS, Net::DNS::Resolver, search functions as is. I can replace them with PEAR Net_DNS module. use Net::DNS; sub...
by: brainbox | last post by:
I have 2 questions: 1.When I read the number using (\S+) its getting stored as 2.08E13. Code I used: if ($line=~/4_begindt_(\S+)/) { print "$1\n" } # $1=2.08E13; instead $1 should contain...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.