?
Solved

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

Posted on 2004-10-16
4
Medium Priority
?
205 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 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