2012-01-05 93 views
1

我有格式的Twitter的飼料:ColdFusion的REReplace(解析Twitter的飼料)

1. Username: Blah blah http://something.com #hashtag 
2. Username: Blah blah http://something.com #hashtag 
3. Username: Blah blah http://something.com #hashtag 

我刪除的用戶名,但我怎麼環繞HTTP標籤(造型)://零件和#hashtags?

這是我目前的ColdFusion代碼:

<cfset feedurl="http://twitter.com/statuses/user_timeline/MyUserID.rss" /> 
    <cffeed 
     source="#feedurl#" 
     properties="feedmeta" 
     query="feeditems" /> 
    <cffeed 
    source="#feedurl#" 
    properties="feedmeta" 
    query="feeditems" /> 
<ul> 
    <cfoutput query="feeditems"> 
     <cfsavecontent variable="twitterString"> 
     #content# 
     </cfsavecontent> 
     <li>#REReplace(twitterString, "UserName: ", "")#</li> 
    </cfoutput> 
</ul> 

回答

1

這爲我工作:

<cfset feedurl="http://twitter.com/statuses/user_timeline/jakefeasel.rss" /> 
    <cffeed 
     source="#feedurl#" 
     properties="feedmeta" 
     query="feeditems" /> 
    <cffeed 
    source="#feedurl#" 
    properties="feedmeta" 
    query="feeditems" /> 
<ul> 
    <cfoutput query="feeditems"> 

     <cfsavecontent variable="twitterString"> 
     #REReplace(content, "UserName: ", "")# 
     </cfsavecontent> 
     <cfset formattedString = twitterString> 

     <cfloop array='#[{"regex" = 'http://\S+', "class" = "URL"}, {"regex" = "##\w+", "class" = "hashTag"}]#' index="regexStruct"> 
      <cfset currentPos = 0> 
      <cfset matches = ReFindNoCase(regexStruct.regex, twitterString, currentPos, true)> 
      <cfloop condition="matches.len[1] IS NOT 0"> 
       <cfset formattedString = Replace(formattedString, mid(twitterString, matches.pos[1], matches.len[1]), "<span class='#regexStruct.class#'>" & mid(twitterString, matches.pos[1], matches.len[1]) & "</span>")> 

       <cfset currentPos = matches.pos[1] + matches.len[1]> 
       <cfset matches = ReFindNoCase(regexStruct.regex, twitterString, currentPos, true)> 
      </cfloop> 
     </cfloop> 

     <li> 
      #formattedString# 
     </li> 
    </cfoutput> 
</ul> 

你會明顯地必須爲 「URL」 和 「#標籤」 類提供的樣式。