Equal mention system Twitter and Facebook

Asked

Viewed 141 times

0

Hello,

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

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
}  

However what I am facing difficulty would be to display in the post already created the mentions.

In case I thought to do 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 really exists so he can run this preg_replace? Any suggestion will be welcome.

  • 1

    Follow this example of what it would be like, and see if that’s what you want. http://ideone.com/ZGoibT

  • Mauro, just in case I understand, he checks for the $result that’s it?

  • Yes, the result will return an array with all users, then you make a loop of repetition, checks one by one if it exists and returns only those that return true.

1 answer

5


Picks up data via 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 will 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 by 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 on IDEONE

  • Ivan Ferrer, then, my question is as follows, in case we assume that the post would be the $text, then he makes a preg_match_all right and search where the tags are, right, then he separates in arrays, and then I make the query to search in the table, but then how do I show as link only existing ones, that this is my initial question.

  • @Viniciushenzel, I edited the question, so you can see how to do.

  • Our @Ivanferrer very good, thank you very much, saved my life!!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.