ameriblog Posted September 13, 2007 Share Posted September 13, 2007 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 https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/ Share on other sites More sharing options...
micah1701 Posted September 13, 2007 Share Posted September 13, 2007 <?php $var = "here is your string of some length"; $spaces = ""; for($i=0; $i<$strlen($var); $i++){ $spaces.=" "; } echo "|" .$spaces . "|"; ?> Link to comment https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347455 Share on other sites More sharing options...
ameriblog Posted September 13, 2007 Author Share Posted September 13, 2007 Thanks, my question is where do I set the max number? If 19 is the longest string, where do I set it so that 19 is the max? Link to comment https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347462 Share on other sites More sharing options...
marcus Posted September 13, 2007 Share Posted September 13, 2007 for($i=0; $i<$strlen($var); $i++){ It's basically tell you the variable is set to 0 and is less than the string length of the variable "var", and it continually added until it reaches that numeral. Link to comment https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347465 Share on other sites More sharing options...
ameriblog Posted September 13, 2007 Author Share Posted September 13, 2007 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 https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347470 Share on other sites More sharing options...
marcus Posted September 13, 2007 Share Posted September 13, 2007 $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 https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347473 Share on other sites More sharing options...
ameriblog Posted September 13, 2007 Author Share Posted September 13, 2007 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 https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347480 Share on other sites More sharing options...
marcus Posted September 13, 2007 Share Posted September 13, 2007 Just interpret mine into yours. Link to comment https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347481 Share on other sites More sharing options...
micah1701 Posted September 13, 2007 Share Posted September 13, 2007 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 https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347502 Share on other sites More sharing options...
Jessica Posted September 13, 2007 Share Posted September 13, 2007 Yes, this is what tables are FOR. TABULAR data. Tables. Please. For the love of pete. Link to comment https://forums.phpfreaks.com/topic/69127-number-of-spaces-based-on-strlen-of-variable/#findComment-347505 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.