Creating hashtags only, nothing else

Discussion in 'PHP' started by qwikad.com, Jun 29, 2016.

  1. #1
    I am using this preg_replace to create hashtags twitter style:

    
    $text = preg_replace('/(^|\s)#(\w*[A-Za-z-_]+\w*)/', '<a href="?query&search=$2">#$2</a>', $text);
    
    Code (markup):
    The problem is it creates hashtags from everything else that has a # sign. So when someone enters an address. i.e.: Some St. Suit #b75 that b75 will become a hashtag.

    Sounds like an impossible task to tell it to differentiate between a real hashtag and something that is not.

    What's even worse, if it finds something like <span style="font-color: #FF0000;"></span> it will turn that font color into a hashtag too.

    So should I just throw that code out or is there a way to make it work?




     
    qwikad.com, Jun 29, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Make the code check for hashtags only at the end of input-text, or create your own input for hashtags, and parse those instead? Hashtags should be at the end,at least in my book :D
    However, it might be very hard to differentiate completely, since something that looks like a hashtag might not be, and vice versa.
     
    PoPSiCLe, Jun 29, 2016 IP