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.