aeroswat Posted December 22, 2009 Share Posted December 22, 2009 I use a function that does this in C++ when outputting text and I was wondering if there is something similar in PHP. What it would do is take a string and find out how long the string is then compare it to a length that you give it and fill in the missing length with blank space for html output. The function would be something like this indent($str,15) And if $str was 12 characters long it would add 3 spaces to the output. Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/ Share on other sites More sharing options...
rajivgonsalves Posted December 22, 2009 Share Posted December 22, 2009 you can try the following function I think it will give you the same effect http://php.net/manual/en/function.str-pad.php Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982520 Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Author Share Posted December 22, 2009 you can try the following function I think it will give you the same effect http://php.net/manual/en/function.str-pad.php Will this work with html output though or will I have to create a function that adds nbsp's to the string? I wrote this I think it'll work function addspace($str, $len){ if(strlen($str)>=$len) {return $str;} for($i=0;$i < (strlen($str)-$len);$i++){ $str+=" "; } return $str; } Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982521 Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Author Share Posted December 22, 2009 So... strlen of that str is returning 20 while $len is equal to 30... Yet it's not getting into the for loop... What's wrong with my logic here? Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982586 Share on other sites More sharing options...
rajivgonsalves Posted December 22, 2009 Share Posted December 22, 2009 hmm.. with what parameters are you calling this, I see that a str_pad won't work as it is a you want to add, you can make it but thats too much of calculations to be done however you can try this, one thing I noticed in your function its adding to the end of the string not to the start. <?php $str = "testtest"; function addspace($str, $len){ if(strlen($str)>=$len) {return $str;} return str_repeat(' ', $len-strlen($str)).$str; } echo addspace($str, 15); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982591 Share on other sites More sharing options...
mrMarcus Posted December 22, 2009 Share Posted December 22, 2009 try this: <?php function addspace($str, $len){ if (strlen($str) >= $len) { return $str; } else { $str_length = strlen($str); for ($i=1; $i<($len-$str_length); $i++){ $str.=" "; } return $str; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982592 Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Author Share Posted December 22, 2009 try this: <?php function addspace($str, $len){ if (strlen($str) >= $len) { return $str; } else { $str_length = strlen($str); for ($i=1; $i<($len-$str_length); $i++){ $str.=" "; } return $str; } } ?> Thanks I'll try it out real fast hmm.. with what parameters are you calling this, I see that a str_pad won't work as it is a you want to add, you can make it but thats too much of calculations to be done however you can try this, one thing I noticed in your function its adding to the end of the string not to the start. <?php $str = "testtest"; function addspace($str, $len){ if(strlen($str)>=$len) {return $str;} return str_repeat(' ', $len-strlen($str)).$str; } echo addspace($str, 15); ?> It's supposed to add to the end of the string It's supposed to do like what a table does by giving me even spacing between my sections Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982594 Share on other sites More sharing options...
rajivgonsalves Posted December 22, 2009 Share Posted December 22, 2009 then the function should have been <?php $str = "testtest"; function addspace($str, $len){ if(strlen($str)>=$len) {return $str;} return $str . str_repeat(' ', $len-strlen($str)); } echo addspace($str, 15); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982596 Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Author Share Posted December 22, 2009 then the function should have been <?php $str = "testtest"; function addspace($str, $len){ if(strlen($str)>=$len) {return $str;} return $str . str_repeat(' ', $len-strlen($str)); } echo addspace($str, 15); ?> works like a charm Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982600 Share on other sites More sharing options...
mrMarcus Posted December 22, 2009 Share Posted December 22, 2009 off of rajivgonsalves's str_repeat .. gives option to add padding to left or right. <?php function addspace($str, $len, $side = 'right') { if (strlen($str) >= $len) { return $str; } else { $space = str_repeat(' ', $len-strlen($str)); return ($side == 'left') ? $space.$str : $str.$space; } } echo addspace ('someword', 15, 'right'); //can omit last argument since padding is defaulted to right side in function; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982602 Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Author Share Posted December 22, 2009 thanks marcus. Your post above made me realize my logic error too lol. I was subtracting the wrong way Seems like they should make this a function Quote Link to comment https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/#findComment-982607 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.