Jump to content

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


ecos

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
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;

?>
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 

Archived

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

×
×
  • 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.