GRMrGecko Posted October 6, 2009 Share Posted October 6, 2009 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 https://forums.phpfreaks.com/topic/176652-twitter-replies-and-emails/ Share on other sites More sharing options...
mrdamien Posted October 6, 2009 Share Posted October 6, 2009 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 https://forums.phpfreaks.com/topic/176652-twitter-replies-and-emails/#findComment-931303 Share on other sites More sharing options...
thebadbad Posted October 6, 2009 Share Posted October 6, 2009 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 https://forums.phpfreaks.com/topic/176652-twitter-replies-and-emails/#findComment-931322 Share on other sites More sharing options...
GRMrGecko Posted October 6, 2009 Author Share Posted October 6, 2009 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 https://forums.phpfreaks.com/topic/176652-twitter-replies-and-emails/#findComment-931466 Share on other sites More sharing options...
thebadbad Posted October 6, 2009 Share Posted October 6, 2009 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 https://forums.phpfreaks.com/topic/176652-twitter-replies-and-emails/#findComment-931483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.