Friday, 11 November 2016

How to build facebook like mentions and hash tag system using php

Mention and hash tag are nice way to reference a user and to make emphasizes. How to build mentions for names like "@username" and hash tag like #hashtag using PHP. This tutorial answers your question, after this tutorial you will able to implement facebook and twitter like mention and hash tag system using php on your web app.
I have already added this function on TheWallClone social networking system. Check my previous post on facebook hash tag and clickable link tutorial
Now for example a user posted ‘Hello @kelvin how are you doing? Join me here #TheWallClone it’s happening
Demo
We are going to use php preg_replace inbuilt function to extract the values attached to @ and # symbol to 

<a href=”yourwebsite.com/kelvin”>@kelvin</a> 
and <a href=”yourwebsite.com/search.php?s=TheWallClone”>#TheWallClone </a> 

as clickable link
PHP CODE

<?php
//extract the values attached to @ and # symbol convert text to clickable links
function convert_clickable_links($message)
{
  $parsedMessage = preg_replace(array('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i'), array('<a href="$1" target="_blank">$1</a>', '$1<a href="http://thewallclone.com/$2">@$2</a>',  '$1<a target="_blank" href="http://thewallclone.com/search.php?s=$2&searching=yes">#$2</a>'), $message);
  return $parsedMessage;
}
?>

HOW TO USE

<?php
//extract the values attached to @ and # symbol convert text to clickable links
function convert_clickable_links($message)
{
  $parsedMessage = preg_replace(array('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i'), array('<a href="$1" target="_blank">$1</a>', '$1<a href="http://thewallclone.com/$2">@$2</a>',  '$1<a target="_blank" href="http://thewallclone.com/search.php?s=$2&searching=yes">#$2</a>'), $message);
  return $parsedMessage;
}

$message="Hello @kelvin how are you doing? Join me here #TheWallClone it’s happening"; 
// string

echo "<p>".convert_clickable_links($message)."</p>"; 
// call function
?>

This works for me, sure it will for every one who is reading this post, this is all i got...

3 comments:

  1. Replies
    1. Bro Good Day!!
      am waiting for the last tutorial i requested for i my data has gone low and i don't know when to contact you again, i would love to hear from you soon
      thanks..

      Delete

Hit me with a comment!