Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/186051-php-function-for-defining-html-space/
Share on other sites

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

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

?>

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

then the function should have been :P

<?php
$str = "testtest";

function addspace($str, $len){
   if(strlen($str)>=$len) {return $str;}
    
   return $str . str_repeat(' ', $len-strlen($str));
}

echo addspace($str, 15);

?>

then the function should have been :P

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

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

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.