PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 23, 2017 1:45 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Dec 09, 2016 11:04 am 
Offline
Forum Newbie

Joined: Fri Dec 09, 2016 10:28 am
Posts: 2
Hi,

I am working a requirement where I need to extract the $ amount or % amount from a string. I have been able to extract dollar amount successfully using the following regex pattern

$pattern = '/\$[0-9]*\.{0,1}[0-9]+/';

I still however need help with finding % amount using the same regex. Can someone please help.

Example:

5% Off Your Order --> Need to get 5% from this
5.25% Off All Orders --> Need to get 5.25% from this
$10 Off Orders Over $100 --> Need to get $10, $100

Can someone please help me with the Regex.


Top
 Profile  
 
PostPosted: Fri Dec 09, 2016 12:07 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13447
Location: New York, NY, US
Why don't you just match [0-9\.] and return all matches. That will work for your case. For any decimal number search for regexps e.g. \d+(\.\d{1,2})?

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Dec 09, 2016 7:06 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6587
Location: WA, USA
Is there a risk of the string containing numbers that aren't money or percentages?

Anyway, what happened when you tried a regex for the percentage? All you needed to do was replace the $ at the beginning for a % at the end...


Top
 Profile  
 
PostPosted: Sun Dec 11, 2016 2:15 am 
Offline
Forum Newbie

Joined: Fri Dec 09, 2016 10:28 am
Posts: 2
requinix wrote:
Is there a risk of the string containing numbers that aren't money or percentages?

Anyway, what happened when you tried a regex for the percentage? All you needed to do was replace the $ at the beginning for a % at the end...



How do I replace for $ at beginning with % at the end? I tried this

/\[0-9]*\.{0,1}[0-9]+%/ but it is not working to get % amount. Any help on how to get that?


Top
 Profile  
 
PostPosted: Sun Dec 11, 2016 2:20 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6587
Location: WA, USA
You need to move that backslash too. The \ and $ are a pair, and together they represent a literal $ symbol.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group