Im creating a list of Twitter results in PHP, im already using PHP to turn text links in the string returned into clickable links rather than plain text.
In some of the Twitter results the string contains hash tags, simply text prepended with the # charac开发者_开发问答ter.
What i would like to do is take the hastag and convert it into a clickable link.
For instance:
Hello my name is Dan, and im working with the #twitter api
Which I want to turn into:
Hello my name is Dan, and im working with the <a href="http://search.twitter.com/search?q=%23twitter">#twitter</a> api
Notice that in the URL I have to change the # to %23 - I think encode or decode.
Also....
Im already using the following code to create clickable links within the string, is it possible to combine the two RegEx's?
Here is my PHP to convert links to clickable links:
//Convert links in string to links
preg_match('/(http:\/\/[^\s]+)/', $row["content"], $text);
$hypertext = "<a target='_blank' href=\"". $text[0] . "\">" . $text[0] . "</a>";
$newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $row["content"]);
$str = preg_replace('/\#([a-z0-9]+)/i', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $str);
EDIT
In terms of the two different patterns, just do:
$str = preg_replace(array($url_pattern, $hash_pattern), array($url_replacement, $hash_replacement), $str);
精彩评论