4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 PHP: Fixed length ? How ?
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
tyjtyj
Junior WebHelper
Junior WebHelper


Joined: 22 Feb 2002
Posts: 20

PostPosted: Thu Feb 06, 2003 3:02 pm (14 years, 8 months ago) Reply with QuoteBack to Top

How to get this output from

$string = "Test"


print $string


output will be
" Test"

________________________________
Justin Store
http://www.tyjtyj.com
OfflineView User's ProfileFind all posts by tyjtyjSend Personal MessageSend email
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Thu Feb 06, 2003 5:10 pm (14 years, 8 months ago) Reply with QuoteBack to Top

I'm not sure I understand your question. what are you trying to do exactly?

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Thu Feb 06, 2003 5:19 pm (14 years, 8 months ago) Reply with QuoteBack to Top

add a leading space by the look of it...
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
tyjtyj
Junior WebHelper
Junior WebHelper


Joined: 22 Feb 2002
Posts: 20

PostPosted: Fri Feb 07, 2003 5:35 am (14 years, 8 months ago) Reply with QuoteBack to Top

Yes a Fixed Wide String

From this string
Code:
"Test"


to this
Code:
"    Test"


8 character string


thank you
hope you understand

________________________________
Justin Store
http://www.tyjtyj.com
OfflineView User's ProfileFind all posts by tyjtyjSend Personal MessageSend email
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Fri Feb 07, 2003 6:59 am (14 years, 8 months ago) Reply with QuoteBack to Top

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.

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
tyjtyj
Junior WebHelper
Junior WebHelper


Joined: 22 Feb 2002
Posts: 20

PostPosted: Fri Feb 07, 2003 1:11 pm (14 years, 8 months ago) Reply with QuoteBack to Top

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
OfflineView User's ProfileFind all posts by tyjtyjSend Personal MessageSend email
drathbun
WebHelper
WebHelper


Joined: 01 Mar 2003
Posts: 69
Location: Texas

PostPosted: Sun Mar 02, 2003 2:54 am (14 years, 7 months ago) Reply with QuoteBack to Top

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. Smile

________________________________
Dave
Photography Site :: Query Tools Forum :: Weekend Fun
OfflineView User's ProfileFind all posts by drathbunSend Personal MessageVisit Poster's Website
drathbun
WebHelper
WebHelper


Joined: 01 Mar 2003
Posts: 69
Location: Texas

PostPosted: Sun Mar 02, 2003 3:03 am (14 years, 7 months ago) Reply with QuoteBack to Top

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. Very Happy

________________________________
Dave
Photography Site :: Query Tools Forum :: Weekend Fun
OfflineView User's ProfileFind all posts by drathbunSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.052518 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme