Skip to content

Instantly share code, notes, and snippets.

@rseyf
Last active February 9, 2020 07:59
Show Gist options
  • Save rseyf/1ba7d58ddc63e4cce9ba to your computer and use it in GitHub Desktop.
Save rseyf/1ba7d58ddc63e4cce9ba to your computer and use it in GitHub Desktop.
Twitter-like Hashtag system with Unicode Support in PHP
<?php
/*
* Twitter-like Hashtag system with Unicode and Hashtag Exporting system Support
*
* @author RezaSeyf <rseyf2017@gmail.com>
* @license Free to public access! do what you want to do
* github: https://github.com/rseyf
* twitter : https://twitter.com/rezaseyf2013
* Export the hashtags of a given string
*
* @param string $str
* the input string to be proccess and export the hashtags from it
*
* @param string $outputType
* using 'null' as default will output the full text with href'ed links for hashtags or 'tagsOnly' to just show the hashtags
*
* @return string Returns the full string with linked hashtags or will just give you the hashtags.
*/
function tagExport($str ,$outputType = null)
{
/**
* @var hashtagsArray[]
* An array of string objects for storing hashtags inside it.
*/
$hashtagsArray = array();
/**
*
* @var strArray[]
* An array of string objects that will save the words of the string argument.
*
*/
$strArray = explode(" ",$str);
/**
*
* @var string $pattern
* regular expression pattern for notes
* don't scare! it works! even with unicode characters!
*/
$pattern = '%(\A#(\w|(\p{L}\p{M}?)|-)+\b)|((?<=\s)#(\w|(\p{L}\p{M}?)|-)+\b)|((?<=\[)#.+?(?=\]))%u';
foreach ($strArray as $b)
{
// match the word with our hashtag pattern
preg_match_all($pattern, ($b), $matches);
/**
*
* @var hashtag[]
* An array of string objects that will save the hashtags.
*
*/
$hashtag = implode(', ', $matches[0]);
// add to array if hashtag is not empty
if (!empty($hashtag) or $hashtag != "")
array_push($hashtagsArray, $hashtag);
}
// now we have found all hashtags in the string
// so we have to replace them and built a new string :
foreach ($hashtagsArray as $c)
{
/**
*
* @var string $hashtagTitle
* container for the exported hashtags without # sign (to insert to db or etc)
*/
$hashtagTitle = ltrim($c,"#");
//create links for hashtags
$str = str_replace($c,'<a href="?lookfor='.$hashtagTitle.'">#'.$hashtagTitle.'</a>',$str);
// uncomment the below line to see the functionality.
// echo "$hashtagTitle <br>";
}
// return fulltext with linked hashtags OR return just the hashtags (with # sign)
if ($outputType == "tagsOnly")
return $listOfHashtags = implode(" ",$hashtagsArray);
else
return $str;
}
// Lets use it!
$sometext = "I'm a #Persian #Programmer. <br> My native language is #فارسی";
echo tagExport($sometext);
@mhrlife
Copy link

mhrlife commented Jan 4, 2018

Useful 👍

@Naskalin
Copy link

Naskalin commented Feb 9, 2020

Thank you very much, long struggled with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment