Jump to content

Number of spaces based on strlen of variable


ameriblog

Recommended Posts

I can use strlen() to get the length of a string/variable.

 

What I am trying to accomplish is adding a number of spaces after the variable displays on the screen based on how long it is.

 

For example if my longest strlen() = 6, then for the strings where strlen() = 3, I want to add '   ' after it, where strlen() = 4 I want to add '  ' etc.

 

Thanks

hm, okay. the reason i ask, and maybe there is a better way all together to do this is I have a list that I want in preformatted text that lists schools, then their win and loss record. the length's of the school's name goes from 3 to 19, there are 120 of them.

 

since 19 is the longest i want it displayed, then 3 spaces will separate it and the W column. so if there is a string in the loop that is 3, i need to make sure there 16 spaces after that string when it is displayed.

$long_string = "this is a super dooper long string";
$short_string = "lolwtf";

for($i=1;$i<=strlen($long_string); $i++){
    for($j=1;$j<=strlen($long_string-$short_string); $j++){
    echo " ";
    }
echo $short_string;
}

 

give that a go

 

Here's what I have:

 

SCHOOL                 W    L

 

The string is in a loop, from my query:

 

<?

$ratings3_rs = $conn->Execute ( "SELECT * FROM ncaa_tm ORDER BY team_mpirank ASC" ) or die ( $conn->ErrorMsg() );

while ( ! $ratings3_rs->EOF ) {

<? echo "$name"; ?>   <? echo "$won"; ?>   <? echo "$lost"; ?>

$ratings3_rs->MoveNext(); }

?>

 

The $name variable is the one I need to add spaces after.

 

not to be overly simplistic, but wouldn't this be a perfectly acceptable time to use an HTML table?

 

<table>
<tr>
   <td>School</td>
   <td>Win</td>
   <td>Loss</td>
<tr>
<?php while ( ! $ratings3_rs->EOF ) { ?>
   <td><?php=$name ?></td>
   <td><?php=$won ?></td>
   <td><?php=$lost ?></td>
<tr>
<?php $ratings3_rs->MoveNext(); } ?>
</table>

 

no need to format the spacing. the table witdths will auto fit themeselves and look much cleaner

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.