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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.