Insert automatic link in hashtags

1

I need a system that identifies the word with the # inside a text and adds it to a table.

And another system that identifies the word with # within a text, however, when it appears in the div, the word is linked.

Example of initial text:

  

#Stackoverflow people are great at # programming.

It turns into:

  

The staff at #Stackoverflow are great at # schedule

    
asked by anonymous 13.07.2017 / 01:14

1 answer

3

If the target is just the accents common in Portuguese, it is easy to list them one by one. I would make a regex with the following blocks:

  • A-Za-z case not accented
  • áàâãéèêíïóôõöúç ñ: accentuated vowels of Portuguese, cedilla and others of lambuja, lowercase
  • ÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇÑ : accentuated vowels of Portuguese, cedilla and others of lambuja, upper case spaces

Therefore:

/^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]+$/

Or, leaving the lowercase / uppercase distinction for the implementation:

/^[a-záàâãéèêíïóôõöúçñ ]+$/i

So to use with 0-9, a-z, A-Z and with accents:

<?php
    $text = "O pessoal do #Stackoverflow são ótimos em #programação.";
    $text = preg_replace('/(?<!\S)#([0-9a-zA-ZáàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ_]+)/', '<a href="http://meusite.com/hashtag/$1">#$1</a>', $text);
    echo $text;
?>
    
13.07.2017 / 02:16