ecos Posted September 27, 2006 Share Posted September 27, 2006 Hello,I need help padding a string to a certain length, based on its existing length. I need the string to have a length which is a multiple of 128. If the string is 100 characters long, I need to pad with 28 characters to equal 128. If the string is 208 characters, I need to pad it with 48 characters to equal 256, and so on for 384, 512, 640, ...I know a bit about str_pad(), but I am not sure of a good way to know which multiple of 128 to bump the string length up to.Thanks for any and all help!Ecos Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2006 Share Posted September 27, 2006 [code]<?php $L = strlen($str); $roundedUp = ceil($L/128)* 128; str_pad($str, $roundedUp, ' ');?>[/code] Quote Link to comment Share on other sites More sharing options...
ecos Posted September 27, 2006 Author Share Posted September 27, 2006 Barand, you are a true Guru!!The celi() fucntion looks quite handy. Thanks for the help!Ecos Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted September 27, 2006 Share Posted September 27, 2006 This is a a bit more clunky but it explains the logic a little better.<?php$length = strlen($string);if ($length < 128) { $i = 128 - $length; $c = 0; while ($c < $i) { $c++; $string .= "X"; }}echo $string;?> Quote Link to comment Share on other sites More sharing options...
ecos Posted September 27, 2006 Author Share Posted September 27, 2006 phpORcaffine,Thanks for the help. This is simialr to what I had come up with, but I needed something that could scale with any size string and any multiple of 128.Initially I had a series of similar statements with each multiple, but that was extra clunky.Thanks again,Ecos Quote Link to comment 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.