PHP code example of klkvsk / clearurls

1. Go to this page and download the library: Download klkvsk/clearurls library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

klkvsk / clearurls example snippets


use ClearUrls\ClearUrls;

// Create cleaner with default rules
$cleaner = ClearUrls::createDefault();

// Clean a URL
$result = $cleaner->clean('https://example.com/page?utm_source=twitter&id=123');

echo $result->url; // https://example.com/page?id=123
echo $result->wasModified ? 'Modified' : 'Unchanged'; // Modified

$cleaner = ClearUrls::createDefault();

// Remove UTM parameters
$result = $cleaner->clean(
    'https://example.com/page?utm_source=twitter&utm_medium=social&id=123'
);
echo $result->url; // https://example.com/page?id=123

// Remove Facebook tracking
$result = $cleaner->clean(
    'https://example.com/page?fbclid=abc123&id=456'
);
echo $result->url; // https://example.com/page?id=456

// Remove Google tracking
$result = $cleaner->clean(
    'https://www.google.com/search?q=test&ved=123&ei=456'
);
echo $result->url; // https://www.google.com/search?q=test

$cleaner = ClearUrls::createDefault();

// Extract target URL from Google redirect
$result = $cleaner->clean(
    'https://www.google.com/url?q=https://example.com/target&ved=123'
);
echo $result->url; // https://example.com/target
echo $result->wasRedirected ? 'Yes' : 'No'; // Yes

// Extract target URL from Facebook redirect
$result = $cleaner->clean(
    'https://l.facebook.com/l.php?u=https%3A%2F%2Fexample.com%2Fpage&h=abc'
);
echo $result->url; // https://example.com/page

$cleaner = ClearUrls::createDefault(allowReferralMarketing: true);

$result = $cleaner->clean(
    'https://www.amazon.com/dp/B08ABC123?tag=myaffiliate-20&ref=nav'
);
// With allowReferralMarketing=true: keeps 'tag' parameter
// With allowReferralMarketing=false: removes 'tag' parameter

$result = $cleaner->clean($url);

if ($result->wasModified) {
    echo "Removed tracking parameters\n";
}

if ($result->wasRedirected) {
    echo "Extracted target URL from redirect\n";
}

if ($result->wasBlocked) {
    echo "URL was blocked (completeProvider rule)\n";
}

if ($result->hadAnyAction()) {
    echo "Something was changed\n";
}

$cleaner = ClearUrls::createDefault();

$urls = [
    'https://example.com/page1?utm_source=email',
    'https://example.com/page2?fbclid=abc123',
    'https://example.com/page3?gclid=xyz789',
];

foreach ($urls as $url) {
    $result = $cleaner->clean($url);
    echo "{$url} -> {$result->url}\n";
}

use ClearUrls\ClearUrls;
use ClearUrls\Provider;

$providers = [
    new Provider(
        name: 'custom',
        urlPattern: '#^https?://example\.com#i',
        completeProvider: false,
        rules: [
            '#^tracking$#i',
            '#^session_id$#i',
        ],
        rawRules: [],
        referralMarketing: [],
        exceptions: [],
        redirections: [],
        forceRedirection: false
    ),
];

$cleaner = new ClearUrls($providers);

// Create with default rules
$cleaner = ClearUrls::createDefault(bool $allowReferralMarketing = false): self

// Create with custom providers
$cleaner = new ClearUrls(array $providers, bool $allowReferralMarketing = false)

// Clean a URL
$result = $cleaner->clean(string $url): ClearUrlResult

// Set referral marketing preference
$cleaner->setAllowReferralMarketing(bool $allow): void

readonly class ClearUrlResult {
    public string $url;              // Cleaned URL
    public bool $wasModified;        // Was URL modified?
    public bool $wasBlocked;         // Was URL blocked?
    public bool $wasRedirected;      // Was URL redirected?

    public function hadAnyAction(): bool;  // Any action taken?
}

readonly class Provider {
    public function __construct(
        public string $name,
        public string $urlPattern,
        public bool $completeProvider,
        public array $rules,
        public array $rawRules,
        public array $referralMarketing,
        public array $exceptions,
        public array $redirections,
        public bool $forceRedirection
    );

    public function matchesUrl(string $url): bool;
    public function getRedirection(string $url): ?string;
    public function getAllRules(bool $
bash
vendor/bin/phpunit tests/PerformanceTest.php --verbose
bash
php build/compile-rules.php
bash
php build/compile-rules.php --local