Posted in ColdFusion | Posted on 05-28-2008 | 10,385 views
Sal asks:
just curious what's the best way (or how you handle) to truncate a paragraph to only show say perhaps 500 chars.? I have a newsletter that I'm emailing out, and I only wanna show 500 chars. of each article in the email.
Ah, I love it when folks ask me the "best" way to do things since no matter what I say, I'm not wrong (grin). Seriously though - here are multiple ways to trim text.
Let's first start off with a block of text that we will use for our tests:
2The Constitution is not an instrument for the government to restrain the people, it is an instrument for the people to restrain the government -- lest it come to dominate our lives and interests. Patrick Henry.
3</cfsavecontent>
So the quickest way to trim text is with left:
However if you use this on the text, you get:
The Constitution is not an instrument for the government to restrain the people, it is an instrumen
As you can see, the last word in the trimmed text, instrument, was cut off before the final t. This isn't a horrible thing of course, but it could be done better. ColdFusion does ship with a Wrap function, but that won't crop the text, it will simply break the text into lines of a certain length. It will break the text nicely though, so why not use list functions?
This returns a nicer trim:
The Constitution is not an instrument for the government to restrain the people, it is an
This works nicely, but I kinda feel 'dirty' doing it like this, so why not see if a UDF exists for this? Turns out one does: FullLeft. This UDF lets me do this instead:
In theory it's doing a lot less work than wrap so it should be quicker.
Ok, so we're done, right? Well, what if we modify the quote a bit:
2The <a href="http://www.coldfusionjedi.com">Constitution</a> is <b>not</b> an instrument for the government to restrain the people, it is an instrument for the people to restrain the government -- lest it come to dominate our lives and interests. Patrick Henry.
3</cfsavecontent>
As you can see I've added some HTML to the text. This HTML messes up my count. If I wanted to show 100 characters, I don't think I'd want HTML to count at all. In fact, I probably don't want to show HTML at all. I can fix that easily enough:
Another issue is space. Now this is a contrived example, but it could happen in a live system:
2The <a href="http://www.coldfusionjedi.com">Constitution</a> is <b>not</b>
3
4
5
6
7
8
9
10
11
12
13an
14instrument for the government to restrain the people, it is an instrument for
15the people to restrain the government -- lest it come to dominate our lives and interests.
16
17Patrick Henry.
18</cfsavecontent>
You can use another regex to handle this:
Or conversely, if you use the wrap() function, it takes a 3rd argument to strip out existing line breaks and carriage returns.
Lastly - it sometimes helps to visually flag text that has been trimmed. Normally this is done with a "...". You can mimic this affect like so:
2 <cfset trimmedQuote = fullLeft(quote, 100)>
3 <cfset trimmedQuote &= "...">
4<cfelse>
5 <cfset trimmedQuote = quote>
6</cfif>
7<cfoutput>#trimmedQuote#</cfoutput>
I just check the length of the original quote and conditionally perform a trim and add the "...".


in mssql select the column with something like:
substring(yourTextyCol,1,100)
then stick your "..." after it
;-)
plaintext = ReReplaceNoCase(htmltext, "<[^>]+>"), " ", "all");
Return Left(plaintext , Find(" ", plaintext, 100)) & "…";
Using characters like “é”, “ü”, “etc”. is ok.
...would be converted to this...
Using characters like “é”, “ü”, “etc”. is ok.
I ran into this problem awhile back and created a nice little JavaScript function to do this, but it could easily be done in ColdFusion as well.
Using characters like “é”, “ü”, “etc”. is ok.
SELECT CONCAT( LEFT( TextToSelect, 500 ), '...' ) FROM Blah
I have had trouble wrapping text containing these kind of tags. The problem is when it cuts the text between a start tag and an end tag.
My "question" should have been: What if I want to keep the html-tags without breaking the start/end-tag when wrapping the text.
1) Remove html
2) Find FullLeft(N)
3) If fullLeft(n) ends at "the", go back to original content (with html), find "the", and end there.
That would let you keep the html and wrap at text not including html, but the N value would be <N as you didn't count the html. Another issue is that it wouldn't stop you from ending with <b>the and having an unmatched tag.
You could write code to determine if your fullleft(n) result is inside HTML. This is done by looking for <X> </X> around your result. If you find it, you either move to the end of </x> or go to before <x>.
You would almost need to create some sort of HTML parser for that. Have you ever looked at the HTML source for a ColdFusion error message? If you notice, it adds a bunch of close tags (</b></p></td></tr></table>...) before it adds the Error message source. It's not calculating those tags, it's just adding a bunch of them to be safe and they don't always work.
Most likely you could create a Regular Express to find all the <BLOCK> tags, and if any of them were still open, you could add their closing tags to the end. I think that would be crazy complicated and would have to ask if it's worth it.
http://www.bennadel.com/blog/982-Ask-Ben-Closing-X...
(1 or more spaces)(link including closing a tag)(1 or more spaces)
and replace with
(link)
Let me gtive it a try.
<cfset text = rereplacenocase(text, "[[:space:]]+(<a.*?>.*?</a>)[[:space:]]+"," \1 ")>
If you really want NO space, period, just change the 3rd arg to be just \1, not (space)\1(space).
$('a[@href^="http://"]').attr("target", "_blank");
Or if you want to get fancy:
$('a[@href^="http://"]').attr({target: "_blank", title: "Opens in a new window"});
Hope that's of interest.
$('a[@href^="http://"]').attr({target: "_blank", title: "Opens in a new window"});
http://www.aliaspooryorik.com/blog/index.cfm/e/pos...
:)
I assume I would have to have an ExternalInterface in the flex actionscript to communicate with the jQuery code? It would be great if it would automatically detect and append the code.
I do have to append a user code to the end of each external link, so I am interested in how jQuery works. I haven't started this part of the project yet, where is the best source for this resource?
Thanks for your help.
[Add Comment] [Subscribe to Comments]