easy way to covert 1 (as number) to one (as word)

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
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

easy way to covert 1 (as number) to one (as word)

Post by spamyboy »

is there any easy way to covert 1 (as number) to one (as word) ?
numbers as numbers to numbers as words.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Don't think there's a standard function for that, but you could see if str_replace() or a related function could do something for you.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Yeah, it's exactly what I was asking for is there such function that do that.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

spamyboy wrote:Yeah, it's exactly what I was asking for is there such function that do that.

There is not a function for that...However, it would be fairly simple to write one ;)
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Yeah.. But what if I set replacment for number 1 to one, and there would be number 11 so script would show it as oneone :|
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Here is a start ;)

Code: Select all

function digsToWords( $num ){

	//VALIDATE NUMBER HERE
	
	
	switch( $num ){
	
		case ( $num < 10 ):
			return getNumLessThanTen( $num );
		break;
		case ( $num < 20 ):
			case 11:
				return 'eleven';
			break;
			case 12:
				return 'twelve';
			break;
			case 13:
				return 'thirteen';
			break;
			.
			..
			...
			....
		case ( $num < 30 ):
			return 'twenty ' . getNumLessThanTen( $num );
		break;
		.
		..
		...
		....
	}
}

function getNumLessThanTen( $num ){

	//VALIDATE NUMBER HERE
	
	
	switch( $num ){
	
		case 1:
			return 'one';
		break;
		case 2:
			return 'two';
		break;
		case 3:
			return 'three';
		break;
		.
		..
		...
		....
	}

}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Post Reply