Twitter and Facebook equal mentions system

0

Hello,

I'm trying to make a mentions system the same as Twitter and Facebook, when the user makes a post, everything that comes after the @ will be stored in the "notifications" table.

preg_match_all("/\B@[a-zA-Z0-9]+/i", $comment, $mentions);
$mentions = array_map(function($str){ return substr($str, 1); },           $mentions[0]);
foreach($mentions as $mentionedUser){
if(!valid_username($mentionedUser)){ continue; }
// aqui ficará a tabela de inserir notificações
}  

But what I'm having difficulty would be the time to display in the post already created the mentions.

In case I thought of doing so:

$post = preg_replace('/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '$1<a         href="http://www.meusite.com/$2">@$2</a>', $post);   

However, how do I check if the user actually exists so he can execute this preg_replace? Any suggestions will be welcome.

    
asked by anonymous 18.11.2016 / 19:44

1 answer

5

Get the data through the regular expression:

$texto = '@Felipe @luizao @Patronaltacao abobrinha @hashtag @ textoxyz @NomeAqui';

preg_match_all('/(^|\s)(@\w+)/', $texto, $result);

$data = $result[2];

$data = array_map(function($value) { 
          return array('possible_account_mail' => strtolower(str_replace('@', '', $value)).'@',
                       'parameter_send' => $value,
                       'account_name' => str_replace('@', '', $value),
                       ); 
    }, $data);

preg_replace(,)
print_r($data); 

And then make a query in the bank through the user account:

 $conta = $data[0]['possible_account_mail'];
 "SELECT * from tabela where email LIKE '%$conta%'";

To do this, you'll obviously have to know how to work well with ajax. Now the idea is to replace the string when you find the record ... you can implement this using the autocomplete UI library:

$('#input').autocomplete({
    source: function (request, response) {
        $.getJSON("/path/request/", function (data) {
            response($.map(data.dealers, function (value, key) {
                return {
                    label: value,
                    value: key
                };
            }));
        });
    },
    minLength: 2,
    delay: 100
}); 

Documentation

  

Here is an example of how to use substitution through a method:

<?php
function replaceAccountExists($accounts_request, $texto) {
    preg_match_all('/(^|\s)(@\w+)/', $texto, $result);
    $data = $result[2];
    $data = array_map(function($value) use ($accounts_request) { 
    $account = strtolower(str_replace('@', '', $value));
    $exists = (in_array($account,$accounts_request));
        return array(
                 'parameter_send' => $value,
                 'exists_account'=> $exists,
                 'url' => '<a href="http://www.meusite.com/' 
                          . $account . '">'
                          . $value . '</a>'
        ); 
    }, $data);

    if (!empty($data)) {
       foreach($data as $val) {
           if ($val['exists_account']) {
              $texto = str_replace($val['parameter_send'],$val['url'], $texto);
           }
       }
    }
    return $texto;
   }

   //requisições válidas do banco
   $accounts_request = array(
    'existo',
    'existia',
    'hash2tag'
   );

    $texto = '@Felipe @luizao @Patronaltacao abobrinha @existo @hash2tag @ textoxyz @NomeAqui @existia';
    $texto_novo = replaceAccountExists($accounts_request, $texto); 
    echo $texto_novo;

See in IDEONE

    
18.11.2016 / 20:49