开发者

Display a link only if no link is present using ColdFusion?

So the announcement functionality on our site displays a "read more" link by default using the following code (in part):

<cfif announcement.recordCount gt 0>
<cfloop query="announcement">
<cfoutput>
<td colspan="2"><span class="left">#teaser_text# <a href="/announcements/?id=#id#" title="Read more...">Read more &raquo;</a>

</cfoutput>
</cfloop>

(Note, there is a cfquery statement prior to that, which I excluded for brevity in the code)

What I'm trying to do here is get the "Read More" link to show after the #teaser_text# only if no link is contained within #teaser_text#, so that I can manually add links i开发者_StackOverflow中文版n if needed and remove the automatically generated link.

Any thoughts on a cfif statement that would do this?

Thanks.

EDIT: To clarify, I want to remove "Read more" if ANY link is found within teaser_text.


To only show the read more link if no hyperlink is found within teaser_text, this check is likely to be good enough:

<cfif NOT refindNoCase('<a\s[^>]*?\bhref\s*=',teaser_text) >
    <a href="/announcements/?id=#id#" title="Read more...">Read more &raquo;</a>
</cfif>

If you want to check for URLs, not for hyperlinks, you need to get more fancy.

You also need to remember that this is treating teaser_text as text (not as HTML), so commenting out a link will not prevent it from being found (if that matters, you need to investigate HTML DOM parsers; and there aren't any for CF so you'd need to look at the Java ones).


This should work:

<cfif findnocase('http://', teaser_text) eq 0>
        <a href="/announcements/?id=#id#" title="Read more...">Read more &raquo;</a>    
        </cfif>

If you are placing the links in manually just change the first parameter of the findnocase() function [i.e. htp/https] or use a regex to figure out if it is a url [via: refindnocse() ]

-sean


Something like this should do what you want

<cfif announcement.recordCount gt 0>
<cfloop query="announcement">
<cfif  findnocase("href",anouncement.teaser_text) >
<a href="/announcements/?id=#announcement.id#"> #anouncement.teaser_text# </a>
<cfelse>
<a href="/announcements/?id=#announcement.id#" >   #anouncement.teaser_text#  Read more </a>
</cfif>
</cfloop>
</cfif>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜