Create a Hashtag System with PHP?

Create a Hashtag System with PHP?

Hashtags are essentially a filter, allowing people to see the conversation associated with that word or phrase. Hashtags is alternative to tags.

Twitter is the first site to introduce the hashtag. But now a days sites like facebook, Instagram, Vine & Tumblr are using this feature in all platforms. If you use the hashtag effectively then you will get the contents across different sites together in a single page.

In this exercise I have demonstrate the PHP hashtag conversion system. That system will convert any hast string to clickable link that directly link to hashtag rendering page. I have use regular expression from scrap to build this type of how to convert hash string to click able link. I have received many request from my viewer to make video tutorial on this type of topic. If you have use twitter or Facebook or google plus, on this of social networking site, you have show the link with hash tag, when you have click on that link so at that time you have show the result on string with hash tag on which you have click.

If you want to build this type of system you have to watch this video tutorial on convert hash tag string to click able link. This type of feature mostly used in social network site in which you have see that when you have show to link with pound sign and when you click on that type of link you can find more information or result on which you have clicked word. This way you can get more result on your favorite keyword. This type of link mostly put on keyword, so you can find more result on that keyword, this way we can build a social network.

index.php

 <?php  
 //index.php  
 //Title - Make PHP Hashtag system by using Regular Expression  
 function convertHashtoLink($string)  
 {  
      $expression = "/#+([a-zA-Z0-9_]+)/";  
      $string = preg_replace($expression, '<a href="hashtag.php?tag=$1">$0</a>', $string);  
      return $string;  
 }  
 $string = "#PHP means Hypertext Preprocessor<br />";  
 $string .= '#mysql is a nice database<br />';  
 $string .= "#Jquery is a Javascript Library<br />";  
 //<a href="">PHP</a>  
 $string = convertHashtoLink($string);  
 echo $string;  
 ?>  

hashtag.php

 <?php  
 //hashtag.php  
 if(isset($_GET["tag"]))  
 {  
      $tag = preg_replace("#[^a-zA-Z0-9_]#", '', $_GET["tag"]);  
      echo '<h1>' . $tag . '</h1>';  
      $connect = mysqli_connect("localhost", "root", "", "sharadguru");  
      $query = "SELECT * FROM sharad_guru where article_title LIKE '%".$tag."%'";  
      $result = mysqli_query($connect, $query);  
      if(mysqli_num_rows($result) > 0)  
      {  
           while($row = mysqli_fetch_array($result))  
           {  
                echo '<p>'.$row["article_title"].'</p>';  
           }  
      }  
      else  
      {  
           echo '<p>No Data Found</p>';  
      }  
 }  
 ?>  

what does my hashtag system need to do so that users can implement hashtags.

I presume that you let the users create posts on your site and you want them be able to add #awesome in the content. After posing it becomes a link which goes to a page displaying all the posts that contain that hashtag.

<?php
 
$content = 'Lorem #ipsum dolor sit amet, consectetur #adipiscing elit. Nam elementum, lectus id pellentesque egestas.';
 
// Extract hashtags
preg_match_all('/#(\w+)/', $content, $matches);
$hashtags = $matches[1];
 
// Replace hashtags with links
$content = preg_replace('/#(\w+)/', '<a , $content);

Now you need to have a table with all the hashtags and one table for to make the relation between hashtags and posts.

When you’ll save the post in the database you will also save the hashtags found in the content. Because in the content you’ll have links instead of hashtags you’ll be able to select the correct posts from the database.

Happy coding.


要查看或添加评论,请登录

Sharad chandra Sharma的更多文章

  • Success & Failure just an Illusion

    Success & Failure just an Illusion

    Success n failure these two adjectives used in everyone life. These words are nothing more than a fake illusion.

  • empty() and isset()

    empty() and isset()

    This is just a quick tutorial regarding the empty() and isset() functions for people that are fairly new to the world…

    1 条评论
  • CDN - Introduction

    CDN - Introduction

    CDN – you keep seeing the acronym. Maybe in URLs, maybe on landing pages, but it never quite clicked – what are Content…

  • Secrets of being good PHP developer (Don't share)

    Secrets of being good PHP developer (Don't share)

    PHP has become the most popular programming language for Web applications. Many popular websites are powered by PHP…

  • Polymorphism Not Complicated anymore in PHP…

    Polymorphism Not Complicated anymore in PHP…

    Polymorphism is a long word for a very simple concept. Polymorphism describes a pattern in object oriented programming…

  • if you want to be a professional PHP developer then you must know

    if you want to be a professional PHP developer then you must know

    There are many beginners or even experienced PHP developers who don’t bother to follow the best practices of the…

    2 条评论
  • Why Use PHP Framework ???

    Why Use PHP Framework ???

    Are you Paying Lots of money to developer for using Wordpress with lots of plugging ? Being a PHP web application…

  • 15 Signs Of Problematic Clients You’ll Meet in IT MARKET (Freelancer / Upwork)

    15 Signs Of Problematic Clients You’ll Meet in IT MARKET (Freelancer / Upwork)

    In freelancing, it’s not the number of clients you have that will make or break your freelance business, but the kind…

    1 条评论
  • Tips For Becoming A Successful Software Developer

    Tips For Becoming A Successful Software Developer

    If you are a great developer, there will always be a market for you. However, landing a job in a global, innovative…

  • Important PHP Security Tips You Should Follow

    Important PHP Security Tips You Should Follow

    What do Wikipedia, Twitter, and Facebook have in common? All of them use PHP technology. A survey and revealed that…

社区洞察

其他会员也浏览了