Author |
Message |
tyjtyj
Junior WebHelper
Joined: 22 Feb 2002
Posts: 20
|
Posted:
Thu Feb 06, 2003 3:02 pm (21 years, 2 months ago) |
|
How to get this output from
$string = "Test"
print $string
output will be
" Test" |
________________________________ Justin Store
http://www.tyjtyj.com |
|
|
|
adam
Forum Moderator & Developer
Joined: 26 Jul 2002
Posts: 704
Location: UK
|
Posted:
Thu Feb 06, 2003 5:10 pm (21 years, 2 months ago) |
|
I'm not sure I understand your question. what are you trying to do exactly? |
________________________________ It's turtles all the way down... |
|
|
|
Darren
Team Member
Joined: 05 Feb 2002
Posts: 549
Location: London
|
Posted:
Thu Feb 06, 2003 5:19 pm (21 years, 2 months ago) |
|
add a leading space by the look of it... |
|
|
|
|
tyjtyj
Junior WebHelper
Joined: 22 Feb 2002
Posts: 20
|
Posted:
Fri Feb 07, 2003 5:35 am (21 years, 2 months ago) |
|
Yes a Fixed Wide String
From this string
to this
8 character string
thank you
hope you understand |
________________________________ Justin Store
http://www.tyjtyj.com |
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Fri Feb 07, 2003 6:59 am (21 years, 2 months ago) |
|
Do you want to always add the same white space, or always add enough white space to make it 8 characters?
Maybe you could use strlen to find out the length, and then add whatever number of spaces you need to add to make up the correct number of chars. |
________________________________
|
|
|
|
tyjtyj
Junior WebHelper
Joined: 22 Feb 2002
Posts: 20
|
Posted:
Fri Feb 07, 2003 1:11 pm (21 years, 2 months ago) |
|
yes i have that idea just wonder if there is any shorter script
another idea is
Code: |
$string = "test". " ";
$string = substr($string,0,8);
|
|
________________________________ Justin Store
http://www.tyjtyj.com |
|
|
|
drathbun
WebHelper
Joined: 01 Mar 2003
Posts: 69
Location: Texas
|
Posted:
Sun Mar 02, 2003 2:54 am (21 years, 1 month ago) |
|
How 'bout a line line answer? Assume $foo contains your string, and you want it to be left padded with spaces to a size of 8.
Code: | $foo = sprintf("%8s", $foo); |
If $foo = "ABC" then the results of this will be " ABC". Short and sweet. |
________________________________ Dave
Photography Site :: Query Tools Forum :: Weekend Fun |
|
|
|
drathbun
WebHelper
Joined: 01 Mar 2003
Posts: 69
Location: Texas
|
Posted:
Sun Mar 02, 2003 3:03 am (21 years, 1 month ago) |
|
tyjtyj wrote: | another idea is
Code: |
$string = "test". " ";
$string = substr($string,0,8);
|
|
That's backwards from what you said you wanted. That will put spaces on the right, not on the left. If you want to go this route, rather than the other solution I suggested, you should do this:
Code: | $foo = substr(" " . $foo, -8) |
This assumes that your input string is never longer than 8 characters. The string of blanks is exactly 8 spaces long, and the substr() function starts at the right side of the string (because of the negative start position) to the end of the string.
As is usual with perl, there is more than one way to do it. |
________________________________ Dave
Photography Site :: Query Tools Forum :: Weekend Fun |
|
|
|
|
Page generation time: 0.215494 seconds :: 19 queries executed :: All Times are GMT
Powered by
phpBB 2.0
© 2001, 2002 phpBB Group :: Based on an FI Theme