HashTag system in PHP

Did you ever try converting hashtags in your website into links of a post or comments…?

Did you ever try to learn it from other sources…?

Look how Facebook Team did it in PHP using Regular Expressions…

Today I will teach you how to do that easily in  PHP Regular Expressions.

Lets start now…

First retrieve the content of the post using $_POST in php

<?php

if(isset($_POST['hashtag']))

{

echo convertToHash($_POST['hashtag']);//convertToHash is a function for converting hashtags into urls…

}

function convertToHash($str)

{

//declare a regular expression…

$regex = "/#+([a-zA-Z0-9_.]+)/";

$string = preg_replace($regex,'<a href="http://facebook.com/$1″ target=”_blank”>$0‘, $str);

return $str;

}

?>

To use this function just use the html form…

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s