|
This is a very simple, and effective way to create a hashtag system for your website, whether it be a social network, or whatever.
The code explained:Code:<?php function hashtag($input) { $output = preg_replace("/#([a-zA-Z\w]+)/", "<a href=\"hashtag.php?hash=$1\">#$1</a>", $input); return $output; } # Usage $string = "This is a cool #php tutorial by #Ddnhf"; echo hashtag($string); ?>
Basically what we are doing, is creating a function named "hashtag" that takes the value of the variable "$input". We're using the preg_replace() function to replace the plain text hashtag with a link while setting the string into the variable "$output", now we just simply, return $output,
Explaining the pattern:
[Copy & Pasted from Noupe.com]
Enjoy, hope this helped. Sorry for the poor explanation, I don't do it often.^ The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted
$ Same as with the circumflex symbol, the dollar sign marks the end of a search pattern
. The period matches any single character
? It will match the preceding pattern zero or one times
+ It will match the preceding pattern one or more times
* It will match the preceding pattern zero or more times
| Boolean OR
- Matches a range of elements
() Groups a different pattern elements together
[] Matches any single character between the square brackets
{min, max} It is used to match exact character counts
\d Matches any single digit
\D Matches any single non digit caharcter
\w Matches any alpha numeric character including underscore (_)
\W Matches any non alpha numeric character excluding the underscore character
\s Matches whitespace character
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |