Solved

Regular Expression needed in PHP for matching all but last numbers and comma in a variable

Posted on 2004-10-16
4
197 Views
Last Modified: 2008-02-01
I am working in PHP and using preg_match to try to retrieve the following.

$line = "             Liatris spicata `Floristan Violet' DND 1                     4,111" (yes, there are multiple spaces in there).

Essentially I need to pull out two different variables from this $line: $product, and $quantity, which should be as follows:

$product = "Liatris spicata `Floristan Violet' DND 1" (notice I stripped spaces before and after)
$quantity = "4,111" (I basically want to do a search starting from the last character of the line, moving left until the first SPACE I find, but I DO want to include commas, since this is a quantity variable)

Any help please?  Thanks so much.
0
Comment
Question by:djs120
  • 3
4 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12329376
You can do this with string functions which will be quicker then regex, eg:

$line = "             Liatris spicata `Floristan Violet' DND 1                     4,111";

$pos = strrpos($line," ");
$quantity = substr($line,$pos+1);
$product = trim(substr($line,0,$pos));
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12329391
To test the output:

echo "Product: [".$product."]<br>\n";
echo "Quantity: [".$quantity."]\n";

Note: i added the square brackets to demonstrate the stripped spaces
0
 
LVL 1

Author Comment

by:djs120
ID: 12329431
Thanks Diablo.  That's exactly what I was looking for and it worked perfectly with my script on all the data I am testing it with.  Thanks for responding so quickly.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12329448
no problem :)

|)iablo
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now