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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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