Skip to content

Instantly share code, notes, and snippets.

@kenjiroe
Last active October 26, 2016 10:18
Show Gist options
  • Save kenjiroe/d16d5a5c123f1c59f505c22ede849c5b to your computer and use it in GitHub Desktop.
Save kenjiroe/d16d5a5c123f1c59f505c22ede849c5b to your computer and use it in GitHub Desktop.
Hashtag
<?php
function convertHashtags($str){
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '<a href="hashtag.php?tag=$1">$0</a>', $str);
return($str);
}
$string = "I am #UberSilly and #MegaPlayful online";
$string = convertHashtags($string);
echo $string;
//Usage:Fulltag
if(isset($_GET["tag"])){
$tag = preg_replace('#[^a-z0-9_]#i', '', $_GET["tag"]);
// $tag is now sanitized and ready for database queries here
$fulltag = "#".$tag;
echo $fulltag;
}
#https://www.developphp.com/video/PHP/Hashtag-System-and-Regex-PHP-Programming-Tutorial
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment