News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

ltrim() but not.

Started by Aquilo, January 25, 2004, 07:26:46 AM

Previous topic - Next topic

Aquilo

is there already an internal function that can return a string to a default length?
like I want to make sure the number the is echoed is three digits long like 000
so if you pass 99 it will print 099 or 9 will be 009. this function I made works
ok unless you need it to be 5 places long because it adds and extra place.
just wondering if there was something in php to do this already!

thanks!

echo lpadding(99, 3, 0);

function lpadding ($str, $pla, $chr)
{
if (strlen($str)>=$pla)
return $str;

$tolen = ($pla-strlen($str));
while (strlen($chr) < $tolen)
$chr .= $chr;

return (int) $chr.$str;
}

Grudge

Don't know if I am understanding you correctly but why not just use sprintf?

http://www.zend.com/manual/function.sprintf.php

Something like $str = sprintf("%03d", string_to_format);
I'm only a half geek really...

Aquilo

Yep! exactly what I was looking for! Thanks Grudge!

[Unknown]


Aquilo

 :P I can't believe I over looked that while searching :-[ and I've used it before :(

this just isn't my week i can't connect to sourceforge cvs, I'm forgetting stuff, and now since I upgraded boardmod I'm having problems updating mods because they work one minute and the next they have three tabs on the end messing up the search for lines!

I think me and my computer have a memory leak!

Advertisement: