Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Hashtag System and Regex PHP Programming Tutorial

In this programming exercise you can learn how to create a PHP hashtag conversion system. It will convert any hashtags found in data strings to clickable links that lead to your dynamic hashtag rendering page. You can also get some great insight into how Regular Expression strings work as we construct a regular expression from scratch to perform the conversion. example.php <?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; ?> hashtag.php <?php 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; } ?>
thumb

PHP Videos

Miscellaneous

Website Design Theme Application PHP Cookies CSS TutorialPDO Prepared Statements PHP TutorialPDO Tutorial Connect Database and Query ExampleActive Page Link Button Highlight TutorialGeoPlugin Tutorial Get User Location Information IP DetectionHashtag System and Regex PHP Programming TutorialPagination MySQLi Google Style Paged Results TutorialActive Clickable Link Function preg_replace Programming TutorialGet Favicon From Web Site Link Access External FilesRandom String Generator PHP Function Programming TutorialForce File Download Dialog In Browser TutorialCheck User Sign Up Name Ajax PHP Social Network TutorialFlat File Text Database System CMS Edit Content TutorialFlat File Text Database System Display ContentExperience Level Evaluation Programming TutorialStagger and Limit Dynamic Output in a While Loop Google AdsTwitter Dynamic Goodies Widget TutorialDynamic Google Maps Location TutorialPrepared Statements PHP mysqli Tutorial