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
?>
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.