Slack Bot / Webhook Token Regex for PHP
/^(xoxb|xoxp|xoxa|xoxr|xoxs|xapp)-[A-Za-z0-9\-]{10,200}$|^https://hooks\.slack\.com/services/T[A-Z0-9]+/B[A-Z0-9]+/[A-Za-z0-9]+$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching slack bot / webhook token, ported and verified for PHP. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Slack Bot / Webhook Token
// ReDoS-safe | RegexVault — Security > API Keys & Tokens
define('SLACK_BOT_WEBHOOK_TOKEN_PATTERN', '/^(xoxb|xoxp|xoxa|xoxr|xoxs|xapp)-[A-Za-z0-9\-]{10,200}$|^https:\/\/hooks\.slack\.com\/services\/T[A-Z0-9]+\/B[A-Z0-9]+\/[A-Za-z0-9]+$/');
function validate_slack_bot_webhook_token(string $input): bool {
return (bool) preg_match(SLACK_BOT_WEBHOOK_TOKEN_PATTERN, $input);
}
// Example
var_dump(validate_slack_bot_webhook_token("xoxb-123456789012-123456789012-ABC123def456GHI789jkl0")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
xoxb-123456789012-123456789012-ABC123def456GHI789jkl0 | xox-invalid-format |
xoxp-1234-5678-9012-abcdef1234567890abcdef | slack_token_aBcDeFgHiJkLmNoPqRsTuV |
https://hooks.slack.com/services/T12345/B12345/AbCdEfGhIj | xoxb-short |
When to use this pattern
This pattern is drawn from the Security > API Keys & Tokens category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Bot tokens (xoxb) can access all channels the bot is in. User tokens (xoxp) act as that specific user. A leaked user token is equivalent to that user's password for Slack.
Technical Notes
Token prefixes: xoxb=bot token, xoxp=user token, xoxa=app-level token (legacy), xoxr=refresh token, xoxs=workspace token (legacy), xapp=Socket Mode app token. Webhook URLs are for incoming webhooks only and cannot read messages.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern