Jump to content

Twitter @ replies and emails


GRMrGecko

Recommended Posts

Hello, I'm trying to figure out how to tell the difference between an email and a @username using perg_replace.

 

My current code is

$text = preg_replace("/(\\S+@\\S+\\.\\w+)/i", "<a href=\"mailto:$1\">$1</a>", $text);
$text = preg_replace("/@([A-Za-z0-9_]+)/i", "@<a href=\"http://twitter.com/$1\" target=\"_blank\">$1</a>", $text);

 

This doesn't work when there is actually an email as it thinks the email is the username.

Any help?

 

Thanks,

Mr. Gecko

Link to comment
Share on other sites

Edit: Since I'm not that great with regular expressions, and don't want to spend very long testing... heres a less-regex idea.

 

Untested:

preg_match( '/([.]{1,255}@([.]{1,255})/', $text, $matches);

foreach($matches as $match)
{
     if(filter_var($match, FILTER_VALIDATE_EMAIL)){
          $text = str_replace($match, "<a href=\"mailto:$match\">$match</a>", $text);
     }else{
          $text = str_replace($match, @<a href=\"http://twitter.com/$match\" target=\"_blank\">$match</a>", $text);
     }
}

Find all strings resembling "@(characters)".

Use PHPs function to determine if its an email.

If not an email, its a twitter account.

 

I don't know if twitter has username restrictions (Like not symbols) if so, you'll probably want to test for those again, in the else clause.

Link to comment
Share on other sites

Edited your patterns a bit:

 

$text = preg_replace('~\S+@\S+\.[a-z]+~i', '<a href="mailto:$0">$0</a>', $text);
$text = preg_replace('~(^|\s)@([a-z0-9_]+)~i', '$1@<a href="http://twitter.com/$2" target="_blank">$2</a>', $text);

The email one being this simple will also convert invalid emails, but it should at least work properly now.

Link to comment
Share on other sites

Edited your patterns a bit:

 

$text = preg_replace('~\S+@\S+\.[a-z]+~i', '<a href="mailto:$0">$0</a>', $text);
$text = preg_replace('~(^|\s)@([a-z0-9_]+)~i', '$1@<a href="http://twitter.com/$2" target="_blank">$2</a>', $text);

The email one being this simple will also convert invalid emails, but it should at least work properly now.

 

So why do you use ~ for that? As you can tell, I'm pretty new to regular expression.

Link to comment
Share on other sites

Those are just the pattern delimiters of my preference. They can be any non-white space, non-alphanumeric ASCII character (except a backslash). And it's always smart to use delimiters that don't appear within the pattern, so you don't have to escape them, hence my use of the 'odd' tildes (I have yet to write a pattern matching a tilde). When you're using forward slashes as pattern delimiters, you would have to escape any forward slashes inside the pattern - something that occurs quite often.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.