Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

preg_match_all with (...)*

Posted on 2004-10-08
2
Medium Priority
?
292 Views
Last Modified: 2008-03-06
When I use preg_match_all to get repeated items, such as:

preg_match_all('/([A-Z]{3})(-([A-Z]{2})-([A-Z]{3}))*/', $myinput, $results);

it overwrites the results each time it iterates through the (...)* section.  How can I preserve these, so that when I input a string like "AAA-BB-CCC-DD-EEE" it will give me out items AAA, BB, CCC, DD, EEE?  Right now, it just gives me out AAA, DD, and EEE.

Thanks for any help.
0
Comment
Question by:jimstar
2 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 800 total points
ID: 12265331
preg_match_all can not do what you want.

It matches the whole string AAA-BB-CCC-DD-EEE.
$result[0][0] contains it. What you have to do with the result is:
$parts = explode('-', $result[0][0]);
The $parts contain whatyou want.

the subpaterns ( ) only hold the last matched items, not all possible matches
0
 
LVL 10

Assisted Solution

by:eeBlueShadow
eeBlueShadow earned 200 total points
ID: 12265768
From http://php.net/manual/en/reference.pcre.pattern.syntax.php#regexp.reference.back-references (the paragraph above the BACK REFERENCES heading):

> When a capturing subpattern is repeated, the value captured is the substring that matched the final iteration. For example,
> after (tweedle[dume]{3}\s*)+ has matched "tweedledum tweedledee" the value of the captured substring is "tweedledee".
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

885 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