Simple Question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Simple Question...

Post by Joe »

How could I extract the the penny part of a price. For example if I had:

£51.32

How could I achieve the 32?. I thought regular expressions but not too sure.

Thanks


Joe 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg, something like:

Code: Select all

#£(\d*)(\.(\d*))?[^.\d]#
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Hmm would that be like:

Code: Select all

$alter = preg_match("#£(\d*)(\.(\d*))?[^.\d]#", $order->info['total']);
It does not seem to work, All I get is 0...
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Oh and sorry I forgot to mention, the price actually shows like:

51.32

So would your (feyd's) expression turn out like:

Code: Select all

$alter = preg_match("#(\d*)(\.(\d*))?[^.\d]#", $order->info['total']);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

£(\d*)(\.(\d*))?[^.\d]?
seems to work..
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Does that work without the £ symbol.

Code: Select all

$alter = preg_match("#£(\d*)(\.(\d*))?[^.\d]?#", $order->info['total']);
The original price is 25.50 and with the new expression it shows 1. Perhaps there is a different technique?.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh... preg_match doesn't return the matches like that.. it returns if it found the pattern or not. Use the third argument to get the matches..
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Yeah its ok I got it, thanks. I was wondering though, when you said use the third argument to get the matches do you mean like:

Code: Select all

$alter = preg_match("#£(\d*)(\.(\d*))?[^.\d]?#", $order->info['total'], matches[0]);
Regular expressions is something I STILL need to catch up on, heh.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the doc page: [php_man]preg_match[/php_man]
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

And the non-regex way

Code: Select all

$price = 19.99;
$parts = explode (".", $price);
$dollars = (int) $parts[0];
$cents = (int) $parts[1];
If the variable had $, or another symbol in it, a simple [php_man]str_replace[/php_man]() would solve that problem. :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Generally i don't like regular expressions because of their performance hit...
Well, after a thread on comp.lang.php http://groups.google.be/groups?hl=nl&lr ... 26rnum%3D6

i changed my mind ;)
ongray
Forum Newbie
Posts: 14
Joined: Wed Sep 01, 2004 1:08 am

Post by ongray »

this will work too...
$price = 19.99;
$cent = substr($price, -2); // returns "99"
Post Reply