1

So I haven't really found anything about this topic online while doing my research. Basically all I want to do is allow people to "tag" other users in their post. Like

@Bob, SO is a great place!

Then I'd handle the notification once I know that a username has been tagged and I'd notify the user. So my question is how would I approach this. I'm trying to achieve something like Twitters tagging system where if you click on the tag it'll take you to the users profile. Would I use regex and weed out the username from the @? Are their any libraries for this to make it easier? Any assistance would be great.

HamZa
  • 13,530
  • 11
  • 51
  • 70
  • 1
    Here you go:http://stackoverflow.com/questions/5160307/how-can-i-create-a-tagging-system-using-php-and-mysql – Jatin Jul 07 '14 at 01:30
  • That appears to be more of an article tag. And there's only SQL examples. Not sure how to do it with PHP is my issue @Jatin –  Jul 07 '14 at 01:34
  • @user3100859 So is your question "how to setup a tagging system from scratch?" with sourcecode? Then your question is too broad. If it is about matching `@username` with regex then there is already some duplicates around. I do not see the technical problems __you__ encountered. This site is not a request-for-tutorial site. – HamZa Jul 07 '14 at 01:37
  • 4
    I'm requesting a guideline, not a tutorial. All I want is some direction to head to. As you stated _This site is not a request-for-tutorial site_. Can you explain [this](http://stackoverflow.com/questions/5160307/how-can-i-create-a-tagging-system-using-php-and-mysql) Or maybe [this](http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type), oh and even [this](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)? @HamZa –  Jul 07 '14 at 01:41
  • @user3100859 I don't want to go too deep into the questions you linked. But the first one is quite old (stackoverflow was less strict in the old days), the second one is not a request-for-tutorial and the third one is about explanation of a certain feature of a programming language. You might better go to a [chatroom](http://chat.stackoverflow.com/rooms/11/php) IMO since opinions on how to tackle this might differ. Again, this is not a specific programming problem. So might as well be too broad. – HamZa Jul 07 '14 at 01:46

3 Answers3

2

you can use textntag plugin for @ or # tagging its completely editable and core files can be simply edited according to your specific needs. http://daniel-zahariev.github.io/jquery-textntags

Rameez Rami
  • 4,039
  • 1
  • 22
  • 30
0

The whole logic lies on these few lines. Find all @names on the post, find their emails in your database and send them a notification.

<?php

$id = 551; // id of this post which is unique
$post = "@Bob, SO is a great place! Even @John is here"; // post content
$pattern = '/@([a-zA-Z0-9]+)/'; // the pattern to extract all nicknames (without @)
preg_match_all($pattern, $post, $matches);

// let's select all these users in order to retrieve their emails
$query = "SELECT nickname, email FROM users WHERE nickname IN ('" . implode("', '", $matches[1]) . "')";
$results = $mysqli_query($query);

while($row = $mysqli_fetch_array($results, MYSQLI_ASSOC)){
    // send an email and a link to the post where he/she has been tagged / mentioned 
    mail($row["email"], "Subject", "Hello " . $row["nickname"] . " someone tagged you. <a href='http://domain/posts/" . $id . "'>Take a look</a>");    
}

?>
hex494D49
  • 8,125
  • 3
  • 34
  • 44
0

The regex to get @username without the @ is:

(?<=@)[\p{L}\p{N}]+

This regex assumes that a username only consists of unicode letter characters and numbers. It also works with the german umlaut: Ü,ü,Ä,ä,Ö,ö,ß

Andie2302
  • 4,551
  • 3
  • 19
  • 39