Using the @ symbol to identify users like twitter does
I'm creating my own version of twitter, I have no idea how to get my back end php script to pick up the @membername within the entered text. Including multiple @membername's for example @billy @joseph, @tyrone,@kesha message
or
@billy hit up @tyrone he's bugging @kesha about the money you owe him.
开发者_JAVA技巧Any scripts of use on how I can accomplish this?
What about using a regex and preg_match_all
, like this :
$str = "Including multiple @membername's for example @billy @joseph, @tyrone,@kesha message ";
if (preg_match_all('#(@\w+)#', $str, $m)) {
var_dump($m[1]);
}
Which would give you the following output :
array
0 => string '@membername' (length=11)
1 => string '@billy' (length=6)
2 => string '@joseph' (length=7)
3 => string '@tyrone' (length=7)
4 => string '@kesha' (length=6)
Basically, here, the pattern I used in my regex is matching :
- An
@
character - Any (more than one) character that can be inside a word :
\w+
- About that, see the Backslash page, in the Regular Expressions (Perl-Compatible) section of the PHP manual
There is a PHP library designed to conform to Twitter's standards. It might save you a lot of hassle. It supports autolinking as well as extraction for @screen_names, #hashtags, and @screen_name/lists.
http://github.com/mzsanford/twitter-text-php
I wrote a Wordpress plugin that handles this a long time ago. Here's the function that I use for this.
function convert_twitter_link($content) {
$pattern = '/\@([a-zA-Z0-9_]+) /';
$replace = '<a rel="nofollow" target="_blank" href="http://twitter.com/'.strtolower('\1').'">@\1</a>';
return preg_replace($pattern,$replace,$content);
}
All depends on how you set it up...Can your user names have a space in it? If not, then just simply parse the message string for "@" and pick up all characters that come after until you encounter a space...that's a real simple way to get it done.
The other side of the question is what are you doing with the usernames once you do "find" them?
精彩评论