Coldfusion REReplace(解析Twitter Feed)

时间:2012-01-05 21:50:39

标签: regex coldfusion

我的推文格式为:

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周围包装标签(用于样式化)?

这是我目前的冷冻代码:

 <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 个答案:

答案 0 :(得分: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”和“#标签”类提供样式。