Jump to content

Archived

This topic is now archived and is closed to further replies.

ecos

Padding the length of a string, help! *SOLVED*

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]
<?php
  $L = strlen($str);
 
  $roundedUp = ceil($L/128)* 128;
   
  str_pad($str, $roundedUp, ' ');
?>[/code]

Share this post


Link to post
Share on other sites
Barand, you are a true Guru!!
The celi() fucntion looks quite handy. 
Thanks for the help!
Ecos

Share this post


Link to post
Share on other sites
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;

?>

Share this post


Link to post
Share on other sites
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 

Share this post


Link to post
Share on other sites

×

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.