Using the @ symbol to identify users like twitter

2019-04-11 19:13发布

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.

Any scripts of use on how I can accomplish this?

4条回答
可以哭但决不认输i
2楼-- · 2019-04-11 19:26

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?

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-11 19:38

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

查看更多
够拽才男人
4楼-- · 2019-04-11 19:45

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);
}
查看更多
Evening l夕情丶
5楼-- · 2019-04-11 19:48

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 :

查看更多
登录 后发表回答