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
Follow this example of what it would be like, and see if that’s what you want. http://ideone.com/ZGoibT
– Mauro Alexandre
Mauro, just in case I understand, he checks for the $result that’s it?
– Vinicius Henzel
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.
– Mauro Alexandre