Skip to main content
Known Participant
February 9, 2011
Question

Outputting clean, random text

  • February 9, 2011
  • 2 replies
  • 948 views

Hi all,

On the homepage of my Web site, I have an area where part of an article displays with a link to the full article text. The article that is loaded is random (base don the code below), and limited to a 200-character output. Now, some of my articles have simple HTML tags in them like break and paragraph. What I'm trying to do with the following script is:

  • select a random article from the query
  • strip it of HTML tags (I don't want the formatting preserved in the article preview, but I do want it preserved for the full text)
  • output only 200 characters.
  • link to the full article text

When I try the method below, I get the "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members" error. Can someone help clear this up for me? Thank you!

<cfquery name="GetArticles" datasource="DS">

SELECT ArticleID,ArticleText,Active

FROM News

WHERE Active = -1

ORDER BY ArticleID

</cfquery>

<cfoutput>

#Dateformat(Today, "mmmm d, yyyy")#</strong><br />

<cfset displayRow = randRange(1, GetArticles.recordCount)>

<cfset CleanText = REReplaceNoCase(GetArticles.ArticleText, "</?[^>]*>", "", "all")>

<cfset ShortText = left(CleanText, 200)>

<cfdump var="#ShortText[variables.displayRow]#">...<a href="news/article.cfm?articleid=#GetArticles.ArticleID[variables.displayRow]#"><strong>Read more</strong></a>

</cfoutput>

    This topic has been closed for replies.

    2 replies

    Inspiring
    February 9, 2011

    Your error was caused by not specifying the rownumber when setting the cleantext variable.

    straffenpAuthor
    Known Participant
    February 9, 2011

    Thanks for the feedback, Dan and JMF,

    I'm a CF amateur learning as I go along, so please excuse my ignorance. Dan, to define the rownumber in CleanText, I tried this line of code, but I received an error:

    <cfset CleanText = REReplaceNoCase(GetPrayers.PrayerText, "</?[^>]*>", "", "all")[variables.displayRow]>

    Could you show me how it should be?  Also, then, when I go to output ShortText, the code will look like the following?

    <cfset ShortText = left(CleanText, 200)>
    <cfdump var="#ShortText#">

    Thanks again.

    Inspiring
    February 9, 2011

    Right variable, wrong place.  You want queryname.fieldname[rownumber]. 

    Participating Frequently
    February 9, 2011

    Here's a function I use to strip HTML from content:

    // match from

    http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.asp

    x

    var reTest =

    "</?\w((\s\w(\s=\s(?:"".?""|'.?'|[^'"">\s]))?)+\s|\s)/?>"; //

    "(...*Read more*]]>

    </cfoutput