spyer Posted January 28, 2010 Share Posted January 28, 2010 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 .. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/ Share on other sites More sharing options...
ChemicalBliss Posted January 28, 2010 Share Posted January 28, 2010 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- Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003107 Share on other sites More sharing options...
premiso Posted January 28, 2010 Share Posted January 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003113 Share on other sites More sharing options...
ChemicalBliss Posted January 28, 2010 Share Posted January 28, 2010 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")); ?> -CB- Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003116 Share on other sites More sharing options...
premiso Posted January 28, 2010 Share Posted January 28, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003121 Share on other sites More sharing options...
ChemicalBliss Posted January 28, 2010 Share Posted January 28, 2010 my thing is bigger than yours Yours is bigger than mine.. lol . 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 . And i do the same as you (start typing and post anyway when someone beats me to it) . peace, -CB- Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003129 Share on other sites More sharing options...
spyer Posted January 29, 2010 Author Share Posted January 29, 2010 Thank you both for your help! That was awesome! I just want to point out a small thing below <?php $words[$i] = ($type == "decode")? base64_decode($words[$i]) : base64_decode($words[$i]); ?> -CB- It should be .. you forgot it <?php $words[$i] = ($type == "decode")? base64_decode($words[$i]) : base64_encode($words[$i]); ?> -CB- Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003838 Share on other sites More sharing options...
salathe Posted January 29, 2010 Share Posted January 29, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/190116-endcode-and-decode-strings/#findComment-1003954 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.