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
sonicimpulseAsked:
Who is Participating?
 
azadisaryevCommented:
there are couple of flaws with approach suggested by parikaa:

- the string returned by his getReviewPreview function will be stripped of all punctuation
- it will not work if review text includes html markup - you will have to at least strip all html first (but you will likely have to do this with any other approach to counting words/characters anyway)

personally, i prefer counting characters instead of words (as in most cases i need to output part of a larger text in a limited space, and X words in every string is a different length, and may very well take up more space than i have for it and break out the layout...)

i like using a slightly modified version of abbreviate() UDF from cflib.org: http://www.cflib.org/udf/abbreviate - it strips all html from a string, keeps punctuation intact, and returns a string without breaking words.

attached is an example of how to use it.

Azadi
<!--- some text including html markup --->
<cfset review = "If you are running a <strong>design agency</strong>, your job is very likely to <strong>combine</strong> <em>business development</em>, <em>graphic design</em>, <em>technology</em> and <em>user experience design</em>: a basketful of very different fields. When dealing with clients, one faces the challenge of clearly and effectively communicating the goals and results of the work done in these areas. In this post, we'll provide you with some ideas on sharing information and knowledge with developers and clients - a couple of tips and tricks we've learned from our own experience.">
<!--- compare output from 2 functions --->
<cfoutput>
	<p><strong><strong>Original text:</strong></strong></p>
	<p>#review#</p>
	<p><strong>getReviewPreview() output [25 words]:</strong></p>
	<p>#getReviewPreview(trim(review))#</p>
	<p><strong>abbreviate() output [200 chars]:</strong></p>
	<p>#abbreviate(review, 200)#</p>
</cfoutput>

<!--- FUNCTIONS --->
<!--- abbreviate() udf: --->
<cfscript>
function abbreviate(string,len) {
    var newString = REReplace(string, "<[^>]+>", "", "ALL");
    var lastSpace = 0;
    newString = REReplace(newString, " \s*", " ", "ALL");
    if (len(newString) gt len) {
        newString = left(newString, len-2);
        lastSpace = find(" ", reverse(newString));
        lastSpace = len(newString) - lastSpace;
        newString = left(newString, lastSpace) & " &##8230;";
    }    
    return newString;
}
</cfscript>

<!--- getReviewPreview() function from parikaa: --->
<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>

Open in new window

0
 
parikaaCommented:
0
 
dincaflorinCommented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
parikaaCommented:
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>

Open in new window

0
 
sonicimpulseAuthor Commented:
parikaa,

I'm getting the following error and I'm not sure why.

Variable REVIEW is undefined
0
 
parikaaCommented:
Can you post your query please?
0
 
parikaaCommented:
Also on line 30 change  to  
0
 
Pravin AsarPrincipal Systems EngineerCommented:
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


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.