bladewolf2010 Posted March 6, 2012 Share Posted March 6, 2012 Hey, I have a function that converts my money into gold, silver and copper. (code below) I was wondering if there is a way to make it so if there is 0 copper and\or silver it'd make it not show it. (IE: 200gold 300 silver 000copper make it look like this, 200gold 300 silver) Thanks for the help in advance :) function gold_format($money) { $counter=strlen($money); if ($counter <= 3) { $moneydone=" $money<img src='images/Copper.png'/>"; return $moneydone; } if ($counter <= 6) { $copper= substr($money, -3); $silver= substr($money, 0, -3); $moneydone=" $silver<img src='images/Silver.png'/> $copper<img src='images/Copper.png'/>"; return $moneydone; } else { $copper= substr($money, -3); $mid=($counter-6); $silver= substr($money, $mid, -3); $gold=substr($money, 0, $mid); $moneydone=" $gold<img src='images/Gold.png'/> $silver<img src='images/Silver.png'/> $copper<img src='images/Copper.png'/>"; return $moneydone; } } Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted March 6, 2012 Share Posted March 6, 2012 Use is_null Quote Link to comment Share on other sites More sharing options...
bladewolf2010 Posted March 6, 2012 Author Share Posted March 6, 2012 Could you explain how is_null is used? Quote Link to comment Share on other sites More sharing options...
Arson Posted March 6, 2012 Share Posted March 6, 2012 You best bet for any questions about php functions is to go to PHP.net and search for the function. Each description usually comes with details and plenty of examples for you to figure things out on your own. http://us2.php.net/manual/en/function.is-null.php Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted March 6, 2012 Share Posted March 6, 2012 Well I thought just by the name of the function it would be self explanatory is null or not! Quote Link to comment Share on other sites More sharing options...
lucky3809 Posted March 6, 2012 Share Posted March 6, 2012 is_null() how hard is it to use? Like any other function, really easy. is_null($var)... Quote Link to comment Share on other sites More sharing options...
bladewolf2010 Posted April 9, 2012 Author Share Posted April 9, 2012 Thanks for the advice, got it to do what I needed it to do using ltrim. :) Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted April 9, 2012 Share Posted April 9, 2012 Kinda botch it fix isnt it? Sounds like you couldnt get it to work so your just removing the output. Quote Link to comment Share on other sites More sharing options...
Spudinski Posted April 9, 2012 Share Posted April 9, 2012 I don't even understand the damn function! Why do you have gold, silver and bronze all using a single field? I'm confused as to what is happening here and why you are splitting it 3 times. For simplicity, if nothing else, use 3 fields!? He's assuming three digits in the string, i.e. 100gold, and thus only collecting the values of them, i.e. 100. Flawed in some ways, but it does work if the integer value is above 100 and below 999. @OP: $int_value = preg_replace('[a-zA-z]', '', $string); if (intval($int_value) == 0) { // zero, e.g. not valid. } else // valid Quote Link to comment Share on other sites More sharing options...
mmomaker Posted April 9, 2012 Share Posted April 9, 2012 Hey, I have a function that converts my money into gold, silver and copper. (code below) I was wondering if there is a way to make it so if there is 0 copper and\or silver it'd make it not show it. (IE: 200gold 300 silver 000copper make it look like this, 200gold 300 silver) Thanks for the help in advance :) function gold_format($money) { $counter=strlen($money); if ($counter <= 3) { $moneydone=" $money<img src='images/Copper.png'/>"; return $moneydone; } if ($counter <= 6) { $copper= substr($money, -3); $silver= substr($money, 0, -3); $moneydone=" $silver<img src='images/Silver.png'/> $copper<img src='images/Copper.png'/>"; return $moneydone; } else { $copper= substr($money, -3); $mid=($counter-6); $silver= substr($money, $mid, -3); $gold=substr($money, 0, $mid); $moneydone=" $gold<img src='images/Gold.png'/> $silver<img src='images/Silver.png'/> $copper<img src='images/Copper.png'/>"; return $moneydone; } } I know what you mean. But i don't know how to code except il give you a psuedo code. If copper = "000" then copper = nothing else if silver = "000" then silver = nothing else if gold = "000" then gold = nothing end if xD ? any help there :P Quote Link to comment Share on other sites More sharing options...
Lithium Posted April 9, 2012 Share Posted April 9, 2012 $copper<img src='images/Copper.png'/>"; return $moneydone; maybe something like... $copper = ($copper == "000") ? "" : "<img src='images/Copper.png'/>"; return $moneydone; Quote Link to comment Share on other sites More sharing options...
Spudinski Posted April 9, 2012 Share Posted April 9, 2012 I know what you mean. But i don't know how to code except il give you a psuedo code. If copper = "000" then copper = nothing else if silver = "000" then silver = nothing else if gold = "000" then gold = nothing end if xD ? any help there :P That would parse as valid VB6.0 with a little tweak. Just sayin'. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted April 9, 2012 Share Posted April 9, 2012 function gold_format($money) { $copper = '000'; $silver = '000'; $gold = '000'; $counter = strlen($money); if ($counter <= 3) { $copper = $money; } if ($counter <= 6) { $copper = substr($money, -3); $silver = substr($money, 0, -3); } if ($counter <= 9) { $copper = substr($money, -3); $silver = substr($money, ($counter - 6), -3); $gold = substr($money, 0,($counter - 6)); } return (($copper == '000') ? '' : $copper.' C<img src="" title="" />'). (($silver == '000') ? '' : $silver.' S<img src="" title="" />'). (($gold == '000') ? '' : $gold.' G<img src="" title="" />'); } Still a little sketchy on why you are doing it like this (equally as sketchy as why you expect the param back to front compared to your code), however this should provide the result you're after. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted April 10, 2012 Share Posted April 10, 2012 Not bad. Here's my 5 minute attempt: <?php class metal_amounts { function __construct($money) { $this->_money = strrev($money); $this->_strlen = strlen($this->_money); $this->_copper = substr($this->_money, 0, 3); $this->_silver = (strlen(substr($this->_money, 3, 3)) > 0) ? substr($this->_money, 3, 3) : 0 ; $this->_gold = (strlen(substr($this->_money, 6)) > 0) ? substr($this->_money, 6) : 0 ; } function display_copper() { return strrev($this->_copper); } function display_silver() { return strrev($this->_silver); } function display_gold() { return strrev($this->_gold); } } $money = 1234567890; $metal = new metal_amounts($money); echo '<p>Copper: ' . $metal->display_copper() . '</p>'; echo '<p>Silver: ' . $metal->display_silver() . '</p>'; echo '<p>Gold: ' . $metal->display_gold() . '</p>'; /* OUTPUTS: Copper: 890 Silver: 567 Gold: 1234 */ Considering the output is used in reverse order -- seems logical to use strrev() to start, sort from there and then strrev() back out of it. Smexy; The even more logical, other than not doing it this way, would be to expect the input to be in the same order as the output. :| Quote Link to comment Share on other sites More sharing options...
bladewolf2010 Posted April 10, 2012 Author Share Posted April 10, 2012 My game does not use $ for the currency. It uses gold, silver, and copper. 1000 copper = 1 silver 1000 silver = 1 gold Here is my fixed function now. function gold_format($money) { $counter=strlen($money); if ($counter <= 3) { $moneydone="$money<img src='images/money/Copper.png'/>"; return $moneydone; } if ($counter <= 6) { $copper= substr($money, -3); $silver= substr($money, 0, -3); $copper=ltrim($copper, 0); if ($copper != "") { $moneydone="$silver<img src='images/money/Silver.png'/>$copper<img src='images/money/Copper.png'/>"; } else { $moneydone="$silver<img src='images/money/Silver.png'/>"; } return $moneydone; } else { $copper= substr($money, -3); $mid=($counter-6); $silver= substr($money, $mid, -3); $gold=substr($money, 0, $mid); $copper=ltrim($copper, 0); $silver=ltrim($silver, 0); if ($copper != "") { if ($silver != "") { $moneydone="$gold<img src='images/money/Gold.png'/>$silver<img src='images/money/Silver.png'/>$copper<img src='images/money/Copper.png'/>"; } else { $moneydone="$gold<img src='images/money/Gold.png'/>$copper<img src='images/money/Copper.png'/>"; } } else { $moneydone="$gold<img src='images/money/Gold.png'/>"; } return $moneydone; } } Quote Link to comment Share on other sites More sharing options...
HauntedDawg Posted April 10, 2012 Share Posted April 10, 2012 Why go through so much? function valueFormat($gold = 0, $siver = 0, $copper = 0) { $return = ''; $return .= ($gold) ? number_format($gold).' G <img src="images/Gold.png" /> ' : ''; $return .= ($silver) ? number_format($silver).' S <img src="images/Silver.png" /> ' : ''; $return .= ($copper) ? number_format($copper).' C <img src="images/Copper.png" />' : ''; return $return; } How to use it? valueFormat($gold, $silver, $copper) Or change it to how u want it to output! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.