Closed Lilmac12312 closed 9 years ago
I've wanted to do this for quite a while, and I'll implement it in a future release.
Samerton, this is ItsSteve from Spigot. I have working code for tagging users, it just needs to be modified a bit. Do you want it? I tested it a lot... It works 99.99 (or 100) %.
@fetch404 That'd be really helpful, if you could submit a pull request with the code I'd really appreciate it
@samerton Okay :)
Sorry for not putting the code up, I was having Git problems. Here is the source if you want it (note: you will need to modify this to work with your database setup/model setup)
(note: this is from one of my projects. you can remove the namespace / imports if need be, but you probably shouldn't.) You will have to update any class references.
<?php namespace Fetch404\Core\Services;
use Fetch404\Core\Models\User;
class MentionsParser
{
/**
* Mentions parser
* By: fetch404
* Date: 7/1/2015
* License: MIT
*/
/**
* Create a new instance of MentionsParser.
*
*/
public function __construct()
{
}
/**
* Parse the given HTML to include @username tags.
*
* @param string $value
* @return string
*/
public static function parse($value = '')
{
if (preg_match_all("/\@([A-Za-z0-9\-_!\.\s]+)/", $value, $matches))
{
$matches = $matches[1];
foreach($matches as $possible_username)
{
$user = null;
while((strlen($possible_username) > 0) && !$user)
{
$user = User::where('name', '=', $possible_username)->first();
if ($user)
{
$value = preg_replace("/".preg_quote("@{$possible_username}", "/")."/", "<a href=\"/some/profile/url\"><img src=\"/some/avatar/url\" height=\"25\" width=\"20\" style=\"margin-bottom: 6px;\" /> {$possible_username}</a>", $value);
break;
}
// chop last word off of it
$new_possible_username = preg_replace("/([^A-Za-z0-9]{1}|[A-Za-z0-9]+)$/", "", $possible_username);
if ($new_possible_username !== $possible_username)
{
$possible_username = $new_possible_username;
}
else
{
break;
}
}
}
}
return $value;
}
}
@fetch404 Thanks, I'll work in implementing the class now.
It would be neat to be able to tag users in your post by doing @