I have used the pattern /[a-z0-9_]+/i within the function:
function validate_twitter($username) {
if (eregi('/[a-z0-9_]+/i', $username)) {
return true;
}
}
With this, I test if the input is a valid twitter username, but i’m having difficulties as it is not giving me a valid result.
Can someone help me find a solution.
To validate if a string is a valid Twitter handle:
If you are trying to match
@usernamewithin a string.For example:
RT @username: lorem ipsum @cjoudrey etc...Use the following:
You can use the latter with preg_replace_callback to linkify usernames in a string.
Edit: Twitter also open sourced text libraries for Java and Ruby for matching usernames, hash tags, etc.. You could probably look into the code and find the regex patterns they use.
Edit (2): Here is a PHP port of the Twitter Text Library: https://github.com/mzsanford/twitter-text-php#readme