encode PHP

Status
Not open for further replies.

pumpa

Active Member
33
2011
0
0
Hi WJ friends,

please i want to know to encode for example word "stream" to "\x73\x74r\x65\x61m"

i know how to decode the line by htmlentities and that stuff but i don't know how to encode.

thanks in advance
 
15 comments
Ok here you go.
PHP:
<?php
$all = 'RM50';
// single
$r = 'R';
$m = 'M';
$a = '5';
$b = '0';
$s=strtoHex($all);var_dump($s);
// single
$s=strtoHex($r);var_dump($s);
$s=strtoHex($m);var_dump($s);
$s=strtoHex($a);var_dump($s);
$s=strtoHex($b);var_dump($s);
exit();
exit();

function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}
?>
 
Last edited:
Ok here you go.
PHP:
<?php
$all = 'RM50';
// single
$r = 'R';
$m = 'M';
$a = '5';
$b = '0';
$s=strtoHex($all);var_dump($s);
// single
$s=strtoHex($r);var_dump($s);
$s=strtoHex($m);var_dump($s);
$s=strtoHex($a);var_dump($s);
$s=strtoHex($b);var_dump($s);
exit();
exit();

function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}
?>

damn that's legit....i was l00king for this too.. thanks xD
 
You'll have to prefix it with \x too (\x[0-9a-f]+ is a hex string in php).

Could probably do it without a loop in php5, like so:
PHP:
function x($s) {
    return preg_replace_callback('#.#', function($m) {
        return '\x' . dechex(ord($m[0]));
    }, $s);
}
 
How do you mean?

\x41\x42\x43 isn't the same as \x414243.

The former will output as 'ABC' (in javascript, PHP doesn't use \x), while the latter will output as 'A4243'. These characters match the expression \x[a-f0-9]{2}, so the numbers following the match won't be taken into account.
 
You need to stick with one thing. You are jumping all over . The \X is not used in php and therefore if you wanted to prefix the text by a single \X as an informational comment than one\X would have been sufficient.

He was not talking javascript or anything but a simple php routine.

What you coded won't work on half the servers as set up today. I know it won't on mine.

If you wanted to create a separate topic with a complete routine with the complete classes that would be helpful to those who like to learn and have the up to date software capable of using it.

But adding this to a simple question is like giving instructions for wormhole travel to someone asking about the best transportation available in their city today.

I expect this from some of the other posters here to try and show off their skills but expected more from an experienced coder/anaylst.
 
Last edited:
He said he wanted to convert a given string to the '\x[0-9a-f]{2}' form (which javascript uses, not PHP), which is what both you and I gave working functions for.

As mentioned in my previous post, I was merely giving an example of an alternative, more modern way you could do it using PHP 5.3. I never stated it was the best way in terms of portability.

Every developer knows what language uses the '\x' representation, which is why my assumptions were made. In which case, what I said about needing one occurrence per character remains true. You can also see this from his initial example where he uses it on a per-character basis, implying it is a javascript string or similar.

Have a good day Lock Down, don't be so defensive in future, remember it was just a response.
 
Last edited:
It seems like he wanted a PHP solution to the problem, just the string he's trying to convert to appears to be a javascript string.

What does it matter though, he has the answer now, twice lol.
 
Where was the twice.

Your function with no reference to the rest of the code posted was not an answer and you should really finish what you start.

Let's see the the javascript answer to the question. I am sure he could use it as other members.

Shouldn't take you but a few minutes for a working tested solution.
 
Last edited:
I didn't feel the need to reference the rest of the code as you had already posted it previously, this was simply a potential replacement for the function in that code. Since there was only one function in use, it seemed obvious to me that would be the one to replace. I did finish, thanks to the rest of the code already existing due to your post.

As for the javascript, he asked for it in PHP, not JS. But for your satisfaction:
Code:
var str = 'some string';
str = str.replace(/./g, function(c) { return '\\x' + c.charCodeAt(0).toString(16); });
// str now contains the characters using '\x' notation

However, it won't evaluate the characters. This is due to the '\\x' being evaluated first in the return statement, then the character code (in hex) separately. Meaning the string will literally contain the '\x' notation.

Doing something like the following would result in the '\x' codes being evaluated, leading to the initial string.
Code:
str = str.replace(/./g, function(c) { eval("c = '\\x" + c.charCodeAt(0).toString(16) + "';"); return c; });

As for it being tested, there's no need, I know it works from reading the code.
 
Status
Not open for further replies.
Back
Top