preg_match('/(download-)?(google-chrome|mozilla-firefox)(-free)?/', $string, $match);
The ?
indicates that the group before it is optional. If the prefix or suffix isn't in the string, those capture groups will be empty in $match
.
If you don't actually want to return the groups with the optional prefix and suffix, make them non-capturing groups by putting ?:
at the beginning of the group:
preg_match('/(?:download-)?(google-chrome|mozilla-firefox)(?:-free)?/', $string, $match);
Now $match[1]
will contain the browser name that you want.