Solved

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

Posted on 2004-10-16
4
198 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…

776 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