smerny Posted September 29, 2009 Author Share Posted September 29, 2009 i take it you now understand my objective, red? Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926822 Share on other sites More sharing options...
Philip Posted September 29, 2009 Share Posted September 29, 2009 Got bored & played around with it.... a bit shorter. Â <?php $num="01101.1011"; function formatBinary($string) { $a = explode('.',$string); $count = 0; foreach($a as $p => $x) { $length = strlen($x); $mod = $length%3; if($mod!=0) { if($count==0) $side = STR_PAD_LEFT; else $side = STR_PAD_RIGHT; $x = str_pad($x, $length-$mod+3, '0', $side); } $a[$p] = wordwrap($x,3,' ',true); $count++; } return implode(' . ', $a); } echo formatBinary($num); Takes the string, explodes it based off the delimiter (period in this case) then only on the first set does it pad to the left, the rest it pads to the right with 0s, grouping in triplets with a space in between. Â Here's some example input/outputs: InputOutput 01101.1011001 101 . 101 100 1101101.10111001 101 101 . 101 110 1101101111001 101 101 111 11.01101.1111011 . 011 010 . 111 100 Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926823 Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 actually alex, when you do not have a decimal point, yours does not work correctly (1 becomes 100 instead of 001)... guess i didn't specify there wouldn't always be a decimal point Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926825 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 whale that flash kingphilp (( love that got board so rewrote it hahahaha))  yes i do, it a very clever way off thinking or out skill ing a hacker if i am on the wright lines of thinking?  ill get back to kingphilps version after 200 drinks whale  (( ill never get to think like a real programmer never to hard))  the examples on this page will make me cut my wrist lol Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926827 Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 Philip, yours works with or without decimal, thanks.... and thanks to others as well   red, i have no idea what you are talking about with this whale or whatever Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926828 Share on other sites More sharing options...
Garethp Posted September 29, 2009 Share Posted September 29, 2009 I think redarrow may be drunk Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926831 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 That's interesting. It's cool to see different takes on the problem.  I could make mine shorter, but I added some protection (for example support if there's more than one delimiter), but I noticed that's stupid because there would be other problems with my code if there was one more than one. And if it was just a decimal (as the OP noted) then there would be no need to have more than one.  So removing the pointless stuff, and changing it a bit, I came up with this:  $str = '1011101.1011'; $split = str_split($str); $str = str_pad($str, 3 - array_search('.', $split) % 3 + strlen($str), "0", STR_PAD_LEFT); $str = str_pad($str, 3 - ((count($split) - 1) - array_search('.', $split)) % 3 + strlen($str), "0", STR_PAD_RIGHT); $split = str_split($str); for($i = 0;$i < count($split);$i++) if($split[$i] == '.') $split[$i] = ' . '; else if($i % 4 == 0) array_splice($split, $i, 0, ' '); echo implode($split);  @KingPhilip: Thanks, never knew about str_pad  Edit: Oh I guess it did need support for without a decimal. Oh well, no time to edit it now, going to bed, was fun. Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926832 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 alex i just hit the floor, you not no ing some think dam never.... Â what you mean you forgot about a function... Â (age my son)) Â Â Â Â Â Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926834 Share on other sites More sharing options...
Philip Posted September 29, 2009 Share Posted September 29, 2009 If you wanted to go even shorter on mine: function formatBinary($s) { $p = STR_PAD_LEFT; foreach(explode('.',$s) as $x) { $l = strlen($x); $a[] = wordwrap(str_pad($x, $l-($l%3)+3, '0', $p),3,' ',true); $p = STR_PAD_RIGHT; } return implode(' . ', $a); } $num="01101.1011"; echo formatBinary($num); Â Should still work the same. Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926837 Share on other sites More sharing options...
Psycho Posted September 29, 2009 Share Posted September 29, 2009 I'll throw my hat into the ring function padByOct($value) {   $parts = explode('.', $value);   $pre = str_pad($parts[0], (strlen($parts[0])+((3-strlen($parts[0])%3)%3)), '0', STR_PAD_LEFT);   $post = str_pad($parts[1], (strlen($parts[1])+((3-strlen($parts[1])%3)%3)), '0', STR_PAD_RIGHT);   return preg_replace("/(.{3})/", "\1 ", $pre).'.'.preg_replace("/(.{3})/", " \1", $post); } $str = '1011101.1011'; echo padByOct($str); //Output: 001 011 101 . 101 100 Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926899 Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 seems to be a lot of enthusiasm on this little project lol  mj, i don't really understand regex... can you explain that part for me? Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-926991 Share on other sites More sharing options...
Psycho Posted September 29, 2009 Share Posted September 29, 2009 seems to be a lot of enthusiasm on this little project lol  mj, i don't really understand regex... can you explain that part for me?  Speaking for myself, I like problems where you have to think outside the box. And I like the challenge of trying to solve a problem with an elegant, concise solution.  I'm no RegEx guru, but I can stumble my way through when I have to. This RegEx is not very complicated though. preg_replace() simply finds text that matches the expression of the first parameter and replaces it with the text in the second parameter (with the third parameter being the text to be operated on). The one part that is a little more "advanced" is the use of a back reference. Let me explain.  Here is the RegEx pattern: "/(.{3})/"  The period is a wildcard character that matches any character. The "{3}" states that I want to find three of those, i.e. 3 of any character. So, that will find all groupings of three characters. Then by putting parents around that I create a backreference to the matches. I can then use that backreference in the replacement expression.  The backreferences are referenced using "\1" for the first backreference, and \2 for the second, etc. I only had one backreference so, I just use "\1" in my replacement expression which is "\1 ".  So basically the full line finds all three character parts and replaces then with their original value plus a space. Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927036 Share on other sites More sharing options...
smerny Posted September 30, 2009 Author Share Posted September 30, 2009 I was using King Philips latest one, but then I decided, what about negative numbers? It didnt work too well... so I tried mj's... it's giving me these things () instead of 1's and 0's Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927415 Share on other sites More sharing options...
Alex Posted September 30, 2009 Share Posted September 30, 2009 Negative numbers? Give an example, do you mean like: Â negative numbers within the string, or a negative over all? Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927416 Share on other sites More sharing options...
Philip Posted September 30, 2009 Share Posted September 30, 2009 Negative? You never mentioned that lol Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927418 Share on other sites More sharing options...
smerny Posted September 30, 2009 Author Share Posted September 30, 2009 I reverted back to kingphilips latest one, and just put this code previous:  $input = $_POST['input']; $neg = false; if(strpos($input, "-") === true) { $input = str_replace("-","",$input); $neg = true; }  and added this before the output  ($neg == true ? "-" : "")  but apparently i did something wrong... it wont remove the "-" Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927423 Share on other sites More sharing options...
Philip Posted September 30, 2009 Share Posted September 30, 2009 Well, what's some sample input/outputs you want? Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927426 Share on other sites More sharing options...
smerny Posted September 30, 2009 Author Share Posted September 30, 2009 input: -1.1 output: -001 . 100 Â currently it's giving me: 0-1 . 100 Â but I think I would rather do this outside of your function, just remove the negative sign from $input and instead store it in boolean $neg (basically what i was trying to do in my last post)... it will give me more flexibility for other things i'll want to do Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927432 Share on other sites More sharing options...
smerny Posted September 30, 2009 Author Share Posted September 30, 2009 hey kingphilip, i just made a pretty neat function based off of yours somewhat... Â function bin2octal($s) { $p = STR_PAD_LEFT; foreach(explode('.',$s) as $x) { $l = strlen($x); $a[] = base_convert(str_pad($x, $l-($l%3)+3, '0', $p),2,; $p = STR_PAD_RIGHT; } return implode('.', $a); } Â this function will convert binary numbers into octals, decimal or not... the base_convert function would not work with decimals Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927443 Share on other sites More sharing options...
Philip Posted September 30, 2009 Share Posted September 30, 2009 but I think I would rather do this outside of your function, just remove the negative sign from $input and instead store it in boolean $neg (basically what i was trying to do in my last post)... it will give me more flexibility for other things i'll want to do  That's what I would do. You could do it with regex, like what mjdamato was going for, but just removing it & storing a temp var is fine too.  function formatBinary($s) { $p = STR_PAD_LEFT; if(strpos($s,'-')!==false) { $s = str_replace('-','',$s); $n = true; } foreach(explode('.',$s) as $x) { $l = strlen($x); $a[] = wordwrap(str_pad($x, $l-($l%3)+3, '0', $p),3,' ',true); $p = STR_PAD_RIGHT; } if(isset($n)) return '- '.implode(' . ', $a); return implode(' . ', $a); } $num="-11101.1011"; echo formatBinary($num);  I do realize that the math in str_pad ($l-($l%3)+3) will cause the correct numbers to be padded (e.g. 111 would become 000 111.) If you're concerned about that, just do a quick check to see if the modulus is 0 or not. Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927450 Share on other sites More sharing options...
smerny Posted September 30, 2009 Author Share Posted September 30, 2009 thanks, if anyone cares to see what i did exactly, check it out here.  note that i only did the binary => octal conversion so far... here is sample output: -10111.11011two -010 111 . 110 110 (Group in threes) - 2  7  .  6  6 (Convert the groups to octal) -27.66eight  does anyone know of a good way to line up the binary octals in line 2 with the octals in line 3? i played around with spacing but it's not very accurate especially when there gets to be more numbers.. Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927464 Share on other sites More sharing options...
Psycho Posted September 30, 2009 Share Posted September 30, 2009 I do realize that the math in str_pad ($l-($l%3)+3) will cause the correct numbers to be padded (e.g. 111 would become 000 111.) If you're concerned about that, just do a quick check to see if the modulus is 0 or not. Â I had the same problem and discovered a solution - using modulus twice!. Â str_pad($l + (3-($l%3)%3)) Â Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927478 Share on other sites More sharing options...
Philip Posted September 30, 2009 Share Posted September 30, 2009 Ahh, I didn't even think about trying that... but: Â $l + (3-($l%3)%3) Should be: $l + (3-($l%3))%3 Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927481 Share on other sites More sharing options...
smerny Posted September 30, 2009 Author Share Posted September 30, 2009 i threw that in there and it's not working right with that, giving me:  100 11 . 111  instead of  010 011 . 111 Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927482 Share on other sites More sharing options...
Philip Posted September 30, 2009 Share Posted September 30, 2009 function formatBinary($s) { $p = STR_PAD_LEFT; if(strpos($s,'-')!==false) { $s = str_replace('-','',$s); $n = true; } foreach(explode('.',$s) as $x) { $l = strlen($x); $a[] = wordwrap(str_pad($x, $l + (3-($l%3))%3, '0', $p),3,' ',true); $p = STR_PAD_RIGHT; } if(isset($n)) return '- '.implode(' . ', $a); return implode(' . ', $a); } $num="-1111101.1011"; echo formatBinary($num); Works fine for me Quote Link to comment https://forums.phpfreaks.com/topic/175891-space-every-3-characters/page/2/#findComment-927483 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.