为什么没有显示文本的前200个字符?

时间:2014-04-07 22:26:57

标签: coldfusion coldfusion-9 coldfusion-10

我正在创建一个页面来显示新闻文章列表。我想显示文本的前200个字符。我将我的数据存储在mySQL中。每个人都工作,除了它没有显示文本的前200个字符。它显示大约30个字符。我一直在看我的代码很长一段时间并尝试了“重新放置”方法,但它不起作用。有人能给我一些关于缺少什么的暗示吗?非常感谢!

<!DOCTYPE html>
<html>
<head>
    <title>Almost done?</title>
    <meta charset="UTF-8">
</head>
<body>
    <!--- Page Title --->
    <h3>Article Listings</h3>

    <!--- Page Content --->
    <!--- Display article title, author, date, first 200 characters
    of the article content and a link to view the full article --->
    <div align="left">
        <cfoutput query="myQuery1">
            <b><a href="full_article_view.cfm?ID=#article_ID#" style="color:##000000; text-decoration: none;">#ucase(myquery1.article_title)#</a></b>
            <hr>
            <p style="color:##848181; font-size:12px">#myquery1.article_author# :: #myquery1.article_date#</p>
            <cfset nonhtml=reReplace(myquery1.article_content, "<[^>]*>","","ALL")>
            #LEFT(nonhtml, 200)# ... <a href="full_article_view.cfm?ID=#article_ID#">Read More</a>
            <br><br><br>
        </cfoutput>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码并且我有200个字符,但是我使用带有html的长文本来获取 myquery1.article_content

首先我要解决的是,除了200个字符以外的短文章也不会引发错误:

<! --- ... --->
<cfset nonhtml=reReplace(myquery1.article_content, "<[^>]*>","","ALL")>
<cfif len(nonhtml) GT 200>
  #LEFT(nonhtml, 200)# ... <a href="full_article_view.cfm?ID=#article_ID#">Read More</a>
<cfelse>
  #nonhtml#
</cfif>

其次,我不喜欢这样的事实:单词是分裂的,或者换句话说,句子在单词中被中断。因此,我建议使用此功能:http://www.cflib.org/udf.cfm/fullleft

顺便说一句:雷蒙德·卡姆登对此有一个很酷的article