echo $matches[1][1];
The best way I can help you in trouble shooting which one you want is to have you echo this:
echo "<pre>".print_r($matches, true);
So if we take the snippet I provided in my earlier post from above as an example, that echo line I just showed you produces this:
Array
(
[0] => Array
(
[0] => {
"Mr2", -- [1]
"Net:110 Tot:360 Hrs:0", -- [2]
}
[1] => {
"Mr3", -- [1]
"Net:210 Tot:560 Hrs:0", -- [2]
}
)
[1] => Array
(
[0] =>
"Mr2", -- [1]
"Net:110 Tot:360 Hrs:0", -- [2]
[1] =>
"Mr3", -- [1]
"Net:210 Tot:560 Hrs:0", -- [2]
)
)
Looks confusing, but after I explain it, it should make sense. Since the preg_match_all pattern is #{([}]+)}# what this is doing is two things.
1) The entire pattern not only has be matched for the preg statement to be true, but it (the entire pattern found) is stored into array element 0.. so if you look at the first chunk of array output, you will notice this:
Array
(
[0] => Array
(
[0] => { ....
}
[1] => { ....
}
)
Since the pattern is {([}]+)}, it is storing every complete match that satisfies that pattern into multi dimensional array[0], which contains another array with two entries... each instance of {.....} found in the string.
2) Notice that the pattern has the paranthesis part ([}]+) which is basically saying, capture anything that is not a }, one or more times.. the key word here is 'capture', which is different from a match in that captures can be stored into additional variables...You can use capturing to fine-tune what to look for and store into variables, so once the entire pattern is executed, not only does the regex engine match it, it now has to create a second set of arrays based on what it captured, and that looks like this:
[1] => Array
(
[0] =>
"Mr2", -- [1]
"Net:110 Tot:360 Hrs:0", -- [2]
[1] =>
"Mr3", -- [1]
"Net:210 Tot:560 Hrs:0", -- [2]
)
)
The only difference between the root array element[0] content and root element [1] content is that [1] doesn't have the { and } parts, as this is not included within the capture. So unless you want the results with { and } included in it, you will want to tap into root array element[1] as a start.. then, simply look at which one within this root[1] and see which one you want and use that.
If you are asking why even bother having { and } in the pattern to begin with, these act as guidance to let the regex engine know which part of the string to match / capture.
I hope this is more clear. If you really want to understand regex, I suggest the following:
regex-info
webtoolscollection
http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1