loka1/social-links

There is no license information available for the latest version (v1.1.0) of this package.

Social links validator and sanitizer

Maintainers

Package info

github.com/loka1/social-links-php

pkg:composer/loka1/social-links

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.1.0 2026-04-07 09:56 UTC

This package is auto-updated.

Last update: 2026-04-07 09:56:49 UTC


README

PHP library for validating, sanitizing, and detecting social media profile URLs.

Installation

composer require loka1/social-links

Usage

use SocialLinks\SocialLinks;
use SocialLinks\Type;

$sl = new SocialLinks();

Validate a URL

$sl->isValid('facebook', 'https://www.facebook.com/zuck');    // true
$sl->isValid('twitter', 'https://x.com/elonmusk');            // true
$sl->isValid('instagram', 'https://instagram.com/instagram'); // true
$sl->isValid('linkedin', 'https://linkedin.com/in/satyanadella'); // true

Extract profile ID

$sl->getProfileId('facebook', 'https://www.facebook.com/zuck'); // "zuck"
$sl->getProfileId('twitter', 'https://x.com/elonmusk');         // "elonmusk"

Build a canonical URL

$sl->getLink('facebook', 'zuck');                         // "https://facebook.com/zuck"
$sl->getLink('twitter', 'elonmusk');                      // "https://x.com/elonmusk"
$sl->getLink('linkedin', 'satyanadella', Type::MOBILE);   // "https://linkedin.com/mwlite/in/satyanadella"

Sanitize a messy URL

$sl->sanitize('facebook', 'http://www.facebook.com/zuck/');          // "https://facebook.com/zuck"
$sl->sanitize('twitter', 'https://twitter.com/elonmusk');            // "https://x.com/elonmusk"
$sl->sanitize('instagram', 'https://www.instagram.com/instagram/');  // "https://instagram.com/instagram"
$sl->sanitize('linkedin', 'http://de.linkedin.com/in/loka1/');   // "https://linkedin.com/in/loka1"

Auto-detect a profile

$sl->detectProfile('https://www.facebook.com/zuck');  // "facebook"
$sl->detectProfile('https://x.com/elonmusk');         // "twitter"
$sl->detectProfile('https://instagram.com/instagram');// "instagram"
$sl->detectProfile('https://linkedin.com/in/test');   // "linkedin"
$sl->detectProfile('https://example.com');            // ""

Custom profiles

use SocialLinks\ProfileMatch;
use SocialLinks\Type;

$sl = new SocialLinks(new Config(usePredefinedProfiles: false));
$sl->addProfile('myNetwork', [
    new ProfileMatch(
        match: '(https?://)?mynetwork.com/({PROFILE_ID})/?',
        group: 2,
        type: Type::DESKTOP,
        pattern: 'https://mynetwork.com/{PROFILE_ID}',
    ),
    new ProfileMatch(
        match: '({PROFILE_ID})',
        group: 1,
    ),
]);

$sl->isValid('myNetwork', 'https://mynetwork.com/johndoe'); // true

Configuration

use SocialLinks\Config;

// Disable predefined profiles
new SocialLinks(new Config(usePredefinedProfiles: false));

// Disable input trimming
new SocialLinks(new Config(trimInput: false));

// Allow URLs with query parameters
new SocialLinks(new Config(allowQueryParams: true));

Supported Networks

Network Desktop Mobile
Facebook facebook.com m.facebook.com
Twitter twitter.com, x.com mobile.twitter.com
Instagram instagram.com m.instagram.com
LinkedIn linkedin.com (with localized subdomains) linkedin.com/mwlite

Requirements

  • PHP >= 8.1
  • No runtime dependencies

License

MIT