Other Stuff:
- Track (redlands)
- Follow
- Twittersearch. Embed it anywhere
- Twitterping, "ping" their web site.
- hperf (web site performance tool)
me on twitter:
@metarobert
Home of my projects:
32Hours.com
function make_links_of_hash_references($text){
global $debug_mode;
global $srv_url;
preg_match_all("|#[a-zA-Z_0-9\-]*|", $text, $matches);
foreach($matches as $list){
foreach($list as $match){
$atch=ltrim($match, "#");
$latch=mb_strtolower($atch);
$text=str_replace($match,"#<a target=\"_blank\" href=\"$srv_url/tag/$latch\" title=\"Go to $srv_url/tag/$latch\">$atch</a>",$text);
}
}
return $text;
}
Comments
@thom Yes! $match and
@thom Yes!
$match and $matches are obvious, from there, I was just entertaining myself with the names.
rtrim(match)=atch
strtolower(atch) made me think latch
I'm just easily entertained, I guess. :)
@Thom heh, far from it. I
@Thom heh, far from it. I just happen to know a bit.
@Paul perl programmer by
@Paul perl programmer by trade?
@robert $matches, $match, $latch, $atch - isn't this just a typo waiting to happen?
Nice! Thanks Paul. I really
Nice! Thanks Paul. I really appreciate you taking the time to think that through an help me out. I'll check this out and play around with it a bit. It'll be satisfying to understand this after struggling with the problem the first time through!
Making part of it lowercase
Making part of it lowercase seems a bit tricky until you realise that preg_replace allows both multiple matches and php code in the matches, when you use the e flag:
$text=preg_replace("/\B#([\w\-]*)\b/e", "'#<a target=\"_blank\" href=\"$srv_url/tag/'.strtolower('$1').'\" title=\"Go to $srv_url/tag/'.strtolower('$1').'\">$1</a>'", $text);I also adjusted the regular expression slightly so that it doesn't match URLs with hashes in them.
There might be a few small bugs but hopefully this helps you out.
Hi Paul, I actually tried to
Hi Paul,
I actually tried to work this problem through with a preg_match(). Did I miss a more elegant solution? The problem I was having with that solution revolved around handling multiple matches in the given text, then replacing them with the appropriate tag that has embedded in it both a version of the match with a character removed (e.g. '#'), and a version of the match in lowercase. Let me know if you still think I could go with a preg_replace(). I'd love to know if I just gave up too soon! :-)
Thanks,
robert
This seems like a bit of a
This seems like a bit of a long way to do it - would it not be simpler to just use preg_replace and cut it down to one line of code?