Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Outputting clean, random text

New Here ,
Feb 09, 2011 Feb 09, 2011

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>

829
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 09, 2011 Feb 09, 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)/?>"; //

"(...<a href="news/article.cfm?articleid=#GetArticles.ArticleID[variables.dis playRow]#">*Read more*</a>

</cfoutput

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 09, 2011 Feb 09, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2011 Feb 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 09, 2011 Feb 09, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2011 Feb 09, 2011
LATEST

Got it! Thanks very much, Dan.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources