It seems like you might be conflating a multi-step task, which may ultimately create more trouble in the long run. You'd basically like to do three things:
- Find all anchor tags on a page
- Extract the URL in the href attribute from these tags
- Extract a specific variable in the query string from that URL
There is a number of ways to do this in PHP. Yes, one direct way is using a regular expression, but it's less transparent. For this particular case, you're really data fitting a very small problem, reduces the scalability of your code for future applications.
My suggestion is the implementation of a light DOM parser available from Source Forge called SimpleHTMLDom. Using this parser, you can write much clearer code for the task you're undertaking.
foreach ($dom_object->find('a') as $anchor){
$url = $anchor->href;
$queryArray = array();
parse_str(parse_url($url, PHP_URL_QUERY), $queryArray);
$myVariable = $queryArr['x'];
}
And then of course $myVariable will be the value you're looking to get with that regex.