Is there a neater way of doing this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cneeds
Forum Newbie
Posts: 24
Joined: Tue Sep 14, 2010 1:39 am

Is there a neater way of doing this?

Post by cneeds »

Hi

What is the neater way of doing this?

$space = '                 ';

:mrgreen:

Chris
jazzercising011
Forum Newbie
Posts: 9
Joined: Tue Sep 14, 2010 4:47 am

Re: Is there a neater way of doing this?

Post by jazzercising011 »

hmm... It would help if I knew what you needed it for. If you need it for padding then I think

<span style="padding-right: 50px;"> blah </span>

might help
stuartr
Forum Newbie
Posts: 13
Joined: Sun Sep 12, 2010 11:11 am

Re: Is there a neater way of doing this?

Post by stuartr »

Code: Select all

$space = str_pad(' ', 102, "&nbsp;")
should do it

The only thing to be aware of is that str_pad will count &nbsp; as 6 characters long when working out the pad so the second variable (102 in this example) needs to be set at 6 times the number of spaces you want (in this case 17).

You can also use this directly with a string field by replacing the first variable (" ") in my example with a variable or a text string

e.g.

Code: Select all

$padded_string = str_pad($original_string, 102, "&nbsp;")
PHP reference for str_pad:

Code: Select all

string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.
cneeds
Forum Newbie
Posts: 24
Joined: Tue Sep 14, 2010 1:39 am

Re: Is there a neater way of doing this?

Post by cneeds »

@ jazzercising011

I need a variable indent depending on a value found in a menu record to show menu heirarchy.

Chris
cneeds
Forum Newbie
Posts: 24
Joined: Tue Sep 14, 2010 1:39 am

Re: Is there a neater way of doing this?

Post by cneeds »

Thank you stuartr

This worked perfectly except that it left a space at the beginning of $space so I changed ' ' to ''.

$space = str_pad('', 102, "&nbsp;")

This is the logic where I use this

Code: Select all

      // indent with some spaces depending on menu_level
      // 3 = indent, 6 = number of characters in &nbsp;
      $nof_spaces = ($row['menu_level'] * 3 * 6);
      if (($nof_spaces) > 0) {
         $msg .= substr($space, 0, $nof_spaces);
      }
You know your php!!

Chris
stuartr
Forum Newbie
Posts: 13
Joined: Sun Sep 12, 2010 11:11 am

Re: Is there a neater way of doing this?

Post by stuartr »

cneeds wrote:
You know your php!!
haha - I wish!! :wink:

Seriously I am fairly new to php, but I have a fair amount of experience in another programming language - Progress OpenEdge 4GL, so I tend to approach most problems with how would I solve them using Progress, and then try and find a PHP equivalent - fortunately on this occasion there was a close equivalent.

now all I have to do is to get my head fully around multidimensional arrays in php and I'll get on much faster :banghead:
cneeds
Forum Newbie
Posts: 24
Joined: Tue Sep 14, 2010 1:39 am

Re: Is there a neater way of doing this?

Post by cneeds »

now all I have to do is to get my head fully around multidimensional arrays in php and I'll get on much faster :banghead:
do you mean like

Code: Select all

$array_var['x']['y']['z'] 
?

Chris
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Is there a neater way of doing this?

Post by AbraCadaver »

stuartr wrote:

Code: Select all

$space = str_pad(' ', 102, "&nbsp;")
should do it

The only thing to be aware of is that str_pad will count &nbsp; as 6 characters long when working out the pad so the second variable (102 in this example) needs to be set at 6 times the number of spaces you want (in this case 17).

Code: Select all

$space = str_repeat('&nbsp;', 17);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
cneeds
Forum Newbie
Posts: 24
Joined: Tue Sep 14, 2010 1:39 am

Re: Is there a neater way of doing this?

Post by cneeds »

wow, neater yet!!

Chris
Post Reply