Hi All,
If I have a sentence with 13 words in it, and I'd like to be able to output
and display only the first 8 words (followed by 3 dots) so that it looks like
this:
Original sentence: "Success is the sum of small efforts, repeated day in and
day out"
New sentence: "Success is the sum of small efforts, repeated..."
Is there a custom tag out there that does this?
Thank in very much in advance!
Does anybody know if there's a custom tag out there that I can use to
output/display a certain number of words in a line. For example,
PaulH **AdobeCommunityExpert** - 18 Nov 2006 02:35 GMT
> Hi All,
>
> If I have a sentence with 13 words in it, and I'd like to be able to output
> and display only the first 8 words (followed by 3 dots) so that it looks like
> this:
pretend the text is a list delimited by spaces.
[CJ] - 18 Nov 2006 06:52 GMT
the list method works...but would require looping 8 times (unless there's a way
that i'm overlooking...which, don't get me wrong...is a very distinct
possibility) :)
the code below will do it without looping. it's probably not the most
elegant...i'm not great with regex. i'm guessing a regex guru could do it
without the nested rereplace() functions.
<cfset myString = "Success is the sum of small efforts, repeated day in and
day out" />
<cfset myTeaser = replace(myString, rereplace(myString, '^(?:\S+\s){8}', ''),
'') />
<cfoutput>#trim(myTeaser)#...</cfoutput>
AppDeveloper - 19 Nov 2006 07:30 GMT
Thank you very much, charlie griefer (CJ)!
I'll give it a try when I have a chance.
Thanks.
dempster - 20 Nov 2006 23:16 GMT
I'm not sure that looping is really that much of a problem. I'd try something
like this:
<cfset myString = "Success is the sum of small efforts, repeated day in and
day out">
<CFLOOP INDEX="oneword" FROM="1" TO="8">#ListGetAt(mystring, oneword, " ")#
</CFLOOP>...
Might need to add some error checking in case there are fewer than 8 words.
[CJ] - 20 Nov 2006 23:41 GMT
but why call a function 8 times? surely the cost of doing so would be greater than using the regex (even if i had to resort to a nested regex function)? :)
PaulH **AdobeCommunityExpert** - 21 Nov 2006 04:00 GMT
> but why call a function 8 times? surely the cost of doing so would be
> greater than using the regex (even if i had to resort to a nested regex
> function)? :)
the regex is a "prettier" solution but both perform about the same.
<cfscript>
s=getTickCount();
myString = "Success is the sum of small efforts, repeated day in and day out";
myTeaser = replace(myString, rereplace(myString, '^(?:\S+\s){8}', ''), '');
e=getTickCount()-s;
</cfscript>
<cfoutput>#trim(myTeaser)#... #e# ms</cfoutput>
<br>
<cfscript>
myTeaser="";
s=getTickCount();
for (i=1; i LTE 8; i=i+1) {
myTeaser=myTeaser &" "& listGetAt(myString,i,' ');
}
e=getTickCount()-s;
</cfscript>
<cfoutput>#trim(myTeaser)#... #e# ms</cfoutput>
AppDeveloper - 22 Nov 2006 23:40 GMT
Hi Charlie Griefer (CJ),
The total number of words in "Success is the sum of small efforts, repeated
day in and day out" is 13. How come when i set the count to {8} in <cfset
myTeaser = replace(myString, rereplace(myString, '^(?:\S+\s){8}', ''), '') />,
my output result is "..."?
Thanks for you help!
garyrgi - 27 Nov 2006 19:42 GMT
Paul,
Thanks for clearing that up, I didn't think there would be too much difference in performance.
tclaremont - 27 Nov 2006 20:07 GMT
I have to ask, what is the logic behind the application that prohibits you from
just grabbing the first 46 characters? Statistically, the average length of the
most commonly used words in the English languare is 5. 8 words times 5
characters + 6 spaces between the 8 words would be 46.
So, using the example above:
Start Phrase:
Success is the sum of small efforts, repeated day in and day out
Displayed Phrase:
Success is the sum of small efforts, repeated ...
I am not arguing with what you are trying to do, I am just trying to envision
why eight words is more useful to an end user than 46 characters.
AppDeveloper - 27 Nov 2006 22:21 GMT
Hi tclaremont,
I totally agree with you. As the matter of fact, I'm (already) currenlty using
your approach, where I output the first 50 characters. But the requirements in
my specs indidates that "If possible, display the first 8 words." I guess they
want the the last word to completely end as one word.
Anyway, thank you for your suggestion.