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
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Fri Sep 03, 2004 6:45 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 03, 2004 6:52 pm
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Fri Sep 03, 2004 6:56 pm
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...
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Fri Sep 03, 2004 7:23 pm
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']);
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 03, 2004 7:26 pm
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Fri Sep 03, 2004 7:36 pm
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?.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 03, 2004 8:12 pm
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..
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Fri Sep 03, 2004 8:32 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 03, 2004 8:44 pm
read the doc page: [php_man]preg_match[/php_man]
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Fri Sep 03, 2004 9:33 pm
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.
ongray
Forum Newbie
Posts: 14 Joined: Wed Sep 01, 2004 1:08 am
Post
by ongray » Fri Sep 03, 2004 10:02 pm
this will work too...
$price = 19.99;
$cent = substr($price, -2); // returns "99"