Если просто добавить переменную в атрибут href
, это сработает:
# v-- & instead of & for W3C validation
preg_replace('/(<a\\s+[^>]+href="[^"]*)(")/', '${1}&type=int$2', $page_src);
Но если вам нужна устойчивость кода, в этом случае href="index.php"
изменяется на href="index.php?type=int"
, а не href="index.php&type=int"
, тогда вам потребуется дополнительная проверка:
function process_link_matches ($matches) {
# get rid of full matching string; all parts are already covered
array_shift($matches);
if (preg_match('/\\?[^"]/', $matches[2])) {
# link already contains $_GET parameters
$matches[1] .= '&type=int';
} else {
# starting a new list of parameters
$matches[1] .= '?type=int';
}
return implode($matches);
}
$page_src = preg_replace_callback('/(<a\\s+[^>]+href=")([^"]*)("[^>]*>)/',
'process_link_matches', $page_src);