Regular Expression Help!

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

Regular Expression Help!

Post by Joe »

I was wondering how I can go about taking the price from a sentance. For example if the sentance go's like:

DVD Player (£70.00)

How would I take the price 70.00 without the currency symbol. I know regular expressions should be used here but I can't seem to work out the pattern.

Any help appreciated.


Joe 8)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

A few ways to do it, here's one (i think ;))

Code: Select all

<?php
$string = 'DVD Player (£70.00)';
preg_match('#£([0-9\.]*)#', $string, $matches);
echo $matches[1];
?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks mark but I couldn't seem to get that to work. Any other ways possible?.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

That way worked fine for me, so your $string must be in a different format. Can you post what $string would look like in your case?
Post Reply