javascript - Twitter "@" and "#" linking in PHP -
i trying convert @ , # links when placing code website.
i found jquery code looks use regex search @ , # unsure of how accomplish same thing using php.
the code is:
at: function(tweet) { return tweet.replace(/\b[@@]([a-za-z0-9_]{1,20})/g, function(m, username) { return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>'; }); }, hash: function(tweet) { return tweet.replace(/(^|\s+)#(\w+)/gi, function(m, before, hash) { return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>'; }); }, so if had this:
@bobbarker going #blahblah , @billybob i need search out , following:
<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=bobbarker'">@bobbarker </a> <a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=billybob'">@billybob </a> likewise hash tag:
#blahblah i need search out , following:
<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23blahblah">#blahblah </a> any great!
update
i put php job @ finding first instant of either @ or # doesnt keep looping. how set loop it?
$thetweet = "@bobbarker going #blahblah , @billybob"; if (preg_match("/\b[@@]([a-za-z0-9_]{1,20})/", $thetweet, $matches)) { $thetweet = str_replace($matches[0],'<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' . trim($matches[0]) . '"> ' . trim($matches[0]) . '</a> ',$thetweet); echo $thetweet . '<br />'; } if (preg_match("/(^|\s+)#(\w+)/", $thetweet, $matches)) { $thetweet = str_replace($matches[0],'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' . trim($matches[0]) . '"> ' . trim($matches[0]) . '</a> ',$thetweet); echo $thetweet; } update #2
answered own question :o)
here is:
$thetweet = "@bobbarker going #blahblah , @billybob"; preg_match_all("@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\s+)?)?)?)@", $thetweet, $matches); //take care of http's foreach($matches[0] $tweetshttp){ $thetweet = str_replace($tweetshttp,'<a target="_blank" class="js-display-url" href="' . $tweetshttp . '"> ' . $tweetshttp . '</a> ', $thetweet); } preg_match_all("/\b[@@]([a-za-z0-9_]{1,20})/", $thetweet, $matches); //take care of @'s foreach($matches[0] $tweetsat){ $thetweet = str_replace($tweetsat,'<a target="_blank" class="twtr-atreply" href="http://twitter.com/' . str_replace("@", "", $tweetsat) . '"> ' . $tweetsat . '</a> ', $thetweet); } preg_match_all("/(^|\s+)#(\w+)/", $thetweet, $matches); //take care of #'s foreach($matches[0] $tweetshash){ $thetweet = str_replace($tweetshash,'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' . $tweetshash . '&src=hash"> ' . $tweetshash . '</a> ', $thetweet); }
Comments
Post a Comment