Re: RIGHT Function?

From: Date: Fri, 13 Nov 2009 23:19:56 +0000
Subject: Re: RIGHT Function?
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-299818@lists.php.net to get a copy of this message
sono-io@fannullone.us wrote: > I think I've solved a problem that I had posted back in September. > Here's a recap: > > ====================== > I need to grab a dollar amount from a text field in a MySQL db that > can contain more information than just the price. Here are 4 examples > of what could be in that field: > > 48,(min) > 2.66 > > 24,(min) > 10.50 > > 4,(min) > 104.82 > > 98.56 > > If there is more info in that field than just the price (as in the > first 3 examples), the price is always on the 2nd line. > ====================== > > The following code works with the tests I've given it so far, but I > just want to double check before I go live with it: > > ... > if ($position = strpos($item['unitprice'], ')') ) > $price = "$" . number_format(substr($item['unitprice'], $position + > 1),2); > else > $price = "$" . number_format($item['unitprice'],2); > ... > Well, when you put it that way, I would try this. ... $parts = explode(PHP_EOL, $item['unitprice']); $price = '$'.(( count($parts) > 1 ) ? $parts[0] : $parts[(count($parts)-1)]); ... > Legend: > $item['unitprice'] is coming from a MySQL statement > > I'm grabbing the position of the right parentheses and adding 1 to > it. Then the substr grabs everything from that point to the end of the > string, correct? If there isn't a ")" in the field, then the else > statement should be performed. > > Can anyone see any errors in my code? Would there be a better way > to write this? > > Thanks again, > Frank >

« previous php.general (#299818) next »