Re: RIGHT Function?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



sono-io@xxxxxxxxxxxxx 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
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux