PHP URI scheme正则优化咨询:能否更好适配http/https/shttp等协议?
Great question! Let's walk through how to refine your regex to better validate http, https, and shttp while maintaining support for all the IANA-listed protocols you're targeting. Here are key improvements and fixes:
1. Fix Critical Escaping Bug
First, a quick correction: your original regex has an unescaped dot in the iris scheme pattern (iris(?:.(?:beep|lwz|xpcs?))?). The unescaped . matches any character, which would incorrectly validate invalid schemes like irisxbeep. Fix this by escaping the dot:
iris(?:\.(?:beep|lwz|xpcs?))?
2. Group HTTP-Family Schemes for Clarity & Performance
Since http, https, and shttp are likely the most frequently used schemes in your validation, move them to the front of your alternation list. This lets the regex engine match these common cases first, reducing unnecessary backtracking and improving speed.
Instead of scattering them in the long list, group them explicitly:
https?|shttp
https?efficiently matches bothhttpandhttps(the?makes thesoptional)shttpis kept separate since it's a distinct scheme (Secure HTTP, different from HTTPS)
3. Add Case Insensitivity (Optional)
URI schemes are technically case-insensitive (per RFC 3986), so if you want to allow variations like HTTP, Https, or SHTTP, add the i modifier to your regex. If you require strict lowercase schemes, omit this modifier.
4. Optimize Boundary Matching
Your existing \b word boundary works for most cases, but ensure it doesn't accidentally match partial schemes. For example, if a string contains httpsomething, \b might still trigger a false positive. If you're validating full URIs (not just isolated scheme strings), you can strengthen this by matching the :// separator immediately after the scheme:
/\b(?:https?|shttp|aaas?|about|acap|acct|cap|cid|coaps?(?:\+(?:tcp|ws))?|crid|data|dav|dict|dns|example|file|ftp|geo|go|gopher|h323|iax|icap|im(?:ap)?|info|ipps?|iris(?:\.(?:beep|lwz|xpcs?))?|...):\/\//i
This ensures the scheme is followed by the standard URI separator, eliminating partial matches.
Final Optimized Regex Example
Putting it all together, here's the revised constant definition (with your truncated protocol list completed to cover IANA's permanent schemes):
define('URL_VALIDATION_REGEX', '/\b(?:https?|shttp|aaas?|about|acap|acct|cap|cid|coaps?(?:\+(?:tcp|ws))?|crid|data|dav|dict|dns|example|file|ftp|geo|go|gopher|h323|iax|icap|im(?:ap)?|info|ipps?|iris(?:\.(?:beep|lwz|xpcs?))?|jabber|ldap|mailto|mid|msrp|msrps|mtqp|mupdate|news|nfs|ni|nih|nntp|opaquelocktoken|pop|pres|reload|rtsp|rtsps|rtspu|service|session|sip|sips|sms|snmp|soap.beep|soap.beeps|stun|stuns|tag|tel|telnet|tftp|thismessage|tn3270|tip|turn|turns|tv|urn|vemmi|vnc|ws|wss|xcon|xcon-userid|xmlrpc.beep|xmlrpc.beeps|xmpp|z39.50r|z39.50s)\b/i');
Key Takeaways
- Accuracy: Fixed the unescaped dot to avoid invalid scheme matches.
- Performance: Prioritized high-frequency HTTP-family schemes to reduce regex engine work.
- Flexibility: Added optional case insensitivity to align with URI standards.
- Strictness: Optional
://matching ensures you're validating full URI scheme contexts.
内容的提问来源于stack exchange,提问作者user9525268

