Reputation: 39
I'm trying to capture all Location paths on an apache conf file, to make automatic nginx templates.
The file that I'm reading has something like this
<Location /images/mobile>
SetHandler modperl
PerlOutputFilterHandler Apache2::AMFImageRendering
</Location>
<Location /images/otherroute>
SetHandler modperl
PerlOutputFilterHandler Apache2::AMFImageRendering
</Location>
I almost got the regex working with the "location" match group, I had the following
$file_str = file_get_contents($conf);
preg_match("/<Location\s+(?P<location>.*?)\s*>.*?Apache2::AMFImageRendering.*?<\/Location>/s", $file_str, $matches);
print_r($matches);
The problem is this only get the first location "/images/mobile" inside $matches['location']
Is there anyway to match all locations, without splitting the string or using preg_match with an offset
Thank you
Upvotes: 0
Views: 225
Reputation: 20486
You're looking for preg_match_all()
. This is PHP's answer to the /g
modifier of normal regular expressions. The 3rd parameter passed ($matches
) will now contain an array of global match sets.
$file_str = file_get_contents($conf);
preg_match_all("/<Location\s+(?P<location>.*?)\s*>.*?Apache2::AMFImageRendering.*?<\/Location>/s", $file_str, $matches);
print_r($matches);
// Array (
// [0] => Array
// (
// [0] => SetHandler modperl PerlOutputFilterHandler Apache2::AMFImageRendering
// [1] => SetHandler modperl PerlOutputFilterHandler Apache2::AMFImageRendering
// )
// [location] => Array
// (
// [0] => /images/mobile
// [1] => /images/otherroute
// )
// [1] => Array
// (
// [0] => /images/mobile
// [1] => /images/otherroute
// )
// )
Upvotes: 1