protonlabs / php-adblock-parser
PHP parser for Adblock Plus filters. A fork of abandoned limonte/php-adblock-parser
1.1.5
2023-04-03 09:39 UTC
Requires
- php: >=8.1
- ext-ctype: *
- ext-iconv: *
- jeremykendall/php-domain-parser: ^6.2
Requires (Dev)
README
This is a fork of the abandoned Limonte\AdblockParser.
It has been edited to optimize performance, namely:
- The object containing the rules can be saved to avoid the long time to create it.
- Only run necessary rules; the generic rules and the rules applying specifically to the domain of the url.
It also separates responsibilities of classes better by having factories, services and DTOs.
Usage
To learn about Adblock Plus filter syntax check these links:
-
Get filter rules somewhere: write them manually, read lines from a file downloaded from EasyList, etc.:
$adblockEntries = [ "||ads.example.com^", "@@||ads.example.com/notbanner^$~script", ];
-
Create AdblockRules instance from the rules array:
use Limonte\AdblockParser; $ruleAggregate = (new RuleAggregateFactory(new RuleFactory()))->createFromAdblockEntries($adblockEntries);
-
Use this instance to check if an URL should be blocked or not:
(new RuleApplier())->shouldBlock("http://ads.example.com", $ruleAggregate); // true (new RuleApplier())->shouldBlock("http://non-ads.example.com", $ruleAggregate); // false
Related projects
- Google Safebrowsing PHP library: limonte/google-safebrowsing
- McAfee SiteAdvisor PHP library: limonte/mcafee-siteadvisor
- Check if link is SPAM: limonte/spam-link-analyser
- Python parser for Adblock Plus filters: scrapinghub/adblockparser
- EasyList filter subscription: easylist/easylist