Jump to content

Php function for defining html space


aeroswat

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

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.