Kenny Devorak
asked on
Get X number of words from a string or field
I have a field in a mySQL table that stores an entire review of a restaurant. I want to be able to pull the first 25 words of the review and store it in a variable with out chopping off the letters.
I'm using Coldfusion 9 and mySQL 5
Any help on this would be great.
Thank you,
Ken Devorak
I'm using Coldfusion 9 and mySQL 5
Any help on this would be great.
Thank you,
Ken Devorak
I think the sollution is to set the class for all elements of <p> type, and for those particular <p id="nomargin"> set the class with margins 0
Use something like the code below (may have bugs) ...
<cfquery name="getReview" datasoure="datsource_name">
SELECT review
FROM table
</cfquery>
<cfloop query="getReview">
<cfoutput>
<p>#getReviewPreview(trim(review))#</p>
</cfoutput>
</cfloop>
<cffunction name="getReviewPreview" returntype="string" access="private">
<cfargument name="review_in" required="true" type="String">
<!--- Initialize variables --->
<cfset var retVal = ''>
<cfset var review_preview = ''>
<cfset var counter = 0>
<!--- Create an array of words and store it in "words" --->
<cfset var words = reMatch("[[:word:]]+", trim(arguments.review_in))>
<!--- Loop through array and store into "review_preview" --->
<cfloop index="word" array="#words#">
<!--- Only append "word" if counter is less than or equal to 25 --->
<cfif counter lte 25>
<cfset review_preview = review_preview & ' ' & trim(word)>
<cfelse>
<cfbreak>
</cfif>
<!--- Increment counter --->
<cfset counter++>
</cfloop>
<!--- Set "retVal" = "review_preview" if it has a length --->
<cfif len(review_preview)>
<cfset retVal = review_preview>
</cfif>
<cfreturn retVal>
</cffunction>
ASKER
parikaa,
I'm getting the following error and I'm not sure why.
Variable REVIEW is undefined
I'm getting the following error and I'm not sure why.
Variable REVIEW is undefined
Can you post your query please?
Also on line 30 change to
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In MySQL, you can do something like:
SELECT SUBSTRING_INDEX(theColumn, " ",10) FROM tablename
Basically this will return a string up until the 10th space character in theColumn
SELECT SUBSTRING_INDEX(theColumn,
Basically this will return a string up until the 10th space character in theColumn
http://www.coldfusionjedi.com/index.cfm/2007/8/2/Counting-Word-Instances-in-a-String