fork(1) download
  1. <?php
  2. function replaceAccountExists($accounts_request, $texto) {
  3. preg_match_all('/(^|\s)(@\w+)/', $texto, $result);
  4. $data = $result[2];
  5. $data = array_map(function($value) use ($accounts_request) {
  6. $account = strtolower(str_replace('@', '', $value));
  7. $exists = (in_array($account, $accounts_request));
  8. return array(
  9. 'parameter_send' => $value,
  10. 'exists_account'=> $exists,
  11. 'url' => '<a href="http://w...content-available-to-author-only...e.com/'
  12. . $account . '">'
  13. . $value . '</a>'
  14. );
  15. }, $data);
  16.  
  17. if (!empty($data)) {
  18. foreach($data as $val) {
  19. if ($val['exists_account']) {
  20. $texto = str_replace($val['parameter_send'],$val['url'], $texto);
  21. }
  22. }
  23. }
  24. return $texto;
  25.  
  26. }
  27.  
  28. //requisições válidas do banco
  29. $accounts_request = array(
  30. 'existo',
  31. 'existia',
  32. 'hash2tag',
  33. '234'
  34. );
  35.  
  36. $texto = '@Felipe @luizao @Patronaltacao abobrinha @ 234 @existo @hash2tag @ textoxyz @NomeAqui @existia';
  37. $texto_novo = replaceAccountExists($accounts_request, $texto);
  38. echo $texto_novo;
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
@Felipe @luizao @Patronaltacao abobrinha @ 234 <a href="http://w...content-available-to-author-only...e.com/existo">@existo</a> <a href="http://w...content-available-to-author-only...e.com/hash2tag">@hash2tag</a> @ textoxyz @NomeAqui <a href="http://w...content-available-to-author-only...e.com/existia">@existia</a>