Roll it up into a method and return your result:
function getQueryPiece($queryString) {
preg_match('/\?\=Brand(.*)®/',$queryString,$matches);
return $matches[1];
}
getQueryPiece("?=BrandBottle®"); // string(6) Bottle
getQueryPiece("?=BrandTest®"); // string(4) Test
getQueryPiece("?=BrandJuice®"); // string(5) Juice
This defines only 1 capturing group (the piece of the string between ?=Brand
and ®
). If you need to capture the other parts as well, just wrap each part in parens: '/(\?\=Brand)(.*)(®)/'
However this will alter the position of that piece in the $matches
array. It would be position 2.
I believe the initial problem in your pattern is the result of using '?' unescaped. Question marks have special meanings in regular expressions. Here's a good write up regarding that: Regex question mark