Jump to content

Recommended Posts

Hello everyone!

 

I have a question aboout the possibilty of a function that acts like base64_encode and decode but actually keeps the spaces.

 

For example in base64 encoding "Hello World" would output "SGVsbG8gV29ybGQ=" and i can decode that to "Hello world using base64_decode.

 

What i want is a function to do this "Hello World" would output "Tkwfd Glwpw" and i can decode it with another function back to "Hello World"!

 

Are there any functions or a function that can do that?

 

I hope i didnt confuse the hell outta you guys :D.. Thank you!

Link to comment
https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/
Share on other sites

Aside from writing your own encode/decode function (easier than what it sound but beyond the time scope i have right now), you could just encode each word seperately.

 

<?php

$somestring = "Hello World, This will be encoded";

function encodewithspaces($var){
   $words = explode(" ",$var); // Put words into an array (no spaces)
   
   // Loop each word and encode it
   for($i=0;$i<count($words);$i++){
       $words[$i] = base64_encode($words[$i]);
   }
   return implode(" ",$words); // Return with spaces
}

echo(encodewithspaces($somestring));

?>

 

hope this helps,

-CB-

A function I just created out of my ass to do it:

 

<?php
function decodeSpaces($message, $which="decode") {
$function = (strtolower($which) == "encode"?"base64_encode":"base64_decode");

if (strstr($message, " ") === false) {
	return $function($message);
}

$message = explode(" ", $message);
$decoded = "";
foreach ($message as $msg) {
	$decoded .= $function($msg) . " ";
}

return $decoded;
}

function encodeSpaces($message) {
return decodeSpaces($message, "encode");
}

$message = "This is my message to encode!";
$encMsg = encodeSpaces($message);
$decMsg = decodeSpaces($encMsg);

echo "Encoded $message looks like:<br />$encMsg<br/><br/> Decoded looks like:<br />$decMsg";

?>

 

EDIT:

Just combined some duplicate code.

lol, fair play for writing so much, but the only difference between my code and yours is yours checks wether it needs to be exploded (wether it has spaces).

 

Oh and you wrote a decode function ;).

 

Heres mine, encode and decode in one function.

 

<?php

$somestring = "Hello World, This will be encoded, then decoded.";

function Encode_Decode($var,$type="encode"){
   $words = explode(" ",$var); // Put words into an array (no spaces)
   
   // Loop each word and encode it
   for($i=0;$i<count($words);$i++){
       $words[$i] = ($type == "decode")? base64_decode($words[$i]) : base64_decode($words[$i]);
   }
   return implode(" ",$words); // Return with spaces
}

$encoded = Encode_Decode($somestring);

echo($encoded."<br />");
echo(Encode_Decode($encoded,"decode"));

?>

 

:P

 

-CB-

lol, fair play for writing so much,

-CB-

 

It was not my intention to start a "my thing is bigger than yours" competition. I simply saw that we had 2 different solutions and decided to post mine as well (as I had it written already no use to waste it).

 

EDIT:

And you do not need the if check for the space in my code :) it will decode / encode just fine with out I found out!

my thing is bigger than yours

 

Yours is bigger than mine.. lol :P.

 

I dont mind whos he uses, stuff like this refines code and makes it quicker so please dont be put-out by my reposting new code. If you can make a better code set please do ill use it in my code, please dont feel im looking to be the best respected helper or w/e i really dont care :P.

 

And i do the same as you (start typing and post anyway when someone beats me to it) :P.

 

peace,

-CB-

Thank you both for your help! That was awesome!

 

I just want to point out a small thing below :D

 

<?php

       $words[$i] = ($type == "decode")? base64_decode($words[$i]) : base64_decode($words[$i]);

?>

-CB-

 

It should be :D.. you forgot it :P

 

<?php

       $words[$i] = ($type == "decode")? base64_decode($words[$i]) : base64_encode($words[$i]);

?>

-CB-

What i want is a function to do this "Hello World" would output "Tkwfd Glwpw" and i can decode it with another function back to "Hello World"!

 

It's note quite the same algorithm but you could use str_rot13 (to get Uryyb Jbeyq).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.