Jump to content

HELP: Decimal to text converter script


wiccan8888

Recommended Posts

well you could do it a few ways. heres one:
[code=php:0]
<?php
$string = "12345";

//start to replace values
$string = str_replace("1", "one", $string);
$string = str_replace("2", "two", $string);
$string = str_replace("3", "three", $string);
$string = str_replace("4", "four", $string);
$string = str_replace("5", "five", $string);
$string = str_replace("6", "six", $string);
$string = str_replace("7", "seven", $string);
$string = str_replace("8", "eight", $string);
$string = str_replace("9", "nine", $string);
$string = str_replace("0", "zero", $string);

echo $string; //returns onetwothreefourfive
?>
Or to simplify that:

[code]<?php
$string = "12345";

$numbers = array ('1','2','3','4','5','6','7','8','9','0');
$words = array('one','two','three','four','five','six','seven','eight','nine','zero');

$string = str_replace($numbers, $words, $string);

echo $string;
?>[/code]
[quote author=wiccan8888 link=topic=114364.msg465847#msg465847 date=1163137539]
men, you're both genius...how did you go that far in PHP, any tips please...
[/quote]

Buy a PHP programming book; read it, practice it, learn it.

Buy a MySQL programming book; read it, practice it, learn it.

Buy a system administration book; read it, practice it, learn it.

Google for every thing else after that.
[quote author=mjdamato link=topic=114364.msg465317#msg465317 date=1163055284]
Or to simplify that:

[code]<?php
$string = "12345";

$numbers = array ('1','2','3','4','5','6','7','8','9','0');
$words = array('one','two','three','four','five','six','seven','eight','nine','zero');

$string = str_replace($numbers, $words, $string);

echo $string;
?>[/code]
[/quote]

Nice code!
You can even simplify it just a little more if you want to.

[code]
<?php
$string = "12345";

$words = array('zero','one','two','three','four','five','six','seven','eight','nine');

//array_keys will return an array of all the array's keys.
$string = str_replace(array_keys($words), $words, $string);

echo $string;
?>
[/code]
Actually that's how I learn BEST.  Reading existing code.  Whether it be in a book or on a web site.  Although I HAVE on occasion used a script without really studying it too much, I try to study the code.  I've found that this has helped me more than anything else.
[quote author=doni49 link=topic=114364.msg466301#msg466301 date=1163209076][code]
<?php
$string = "12345";

$words = array('zero','one','two','three','four','five','six','seven','eight','nine');

//array_keys will return an array of all the array's keys.
$string = str_replace(array_keys($words), $words, $string);

echo $string;
?>
[/code]
[/quote]

Absolutely! In fact, using the key/value pairs would make sense even when converting something other than numbers. For example if converting from English to Spanish, using a single array like this:

$words = array ('one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'quatro');

would be better programatically (reduce errors) and from a readability standpoint rather than using two arrays.

@wiccan,

I've never read any books. But, I've read pleanty of tutorials. And, I find personal projects that will require me to learn something new. I find I don't really lear something if I'm only following an example in a book. And a big part are forums such as these. I don't know the answer to a lot of the questions I answer when I first read them. Bt I will do a little research or try something to find a solution if I can.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.