My solution removes now all <link></link>
tags and <style>@import url()</style>
googleapi entries in the given HTML:
add_action( 'wp_footer', 'SPDSGVOPublic::removeGoogleFonts' );
/**
* Remove all occurrences of google fonts
*/
public static function removeGoogleFonts()
{
ob_start();
$content = ob_get_clean();
$patternImportUrl="/(@import[\s]url\((?:"|\")((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)(?:"|\')\)\;)/';
$patternLinkTag = '/<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>/';
preg_match_all($patternImportUrl, $content, $matchesImportUrl);
preg_match_all($patternLinkTag, $content, $matchesLinkTag);
$matches = array_merge($matchesImportUrl,$matchesLinkTag);
foreach( $matches as $match ) {
$content = str_replace( $match, '', $content );
}
echo $content;
}