|
|
|
|
The Ellipsize FunctionPosted May 27th, 2011 02:27 PM I have a bit of code I would like to share. I was looking in CPAN for something similar and of course I found something much more robust in HTML::Truncate. Anyways, you see all the time a preview of the article text and an ellipsis and a link to the entire article.
The problem arises when people use HTML to format the article. Trying to truncate the text requires that you first remove the HTML because you never know which tags you will unbalace by chopping it up. For example lets say you have a paragraph with 1000 characters and you only want to show 500 - you end up removing the closing P tag which causes the HTML to become unbalanced. Obviously this is a simplified example but you can imagine the effects on your web page layout when you have unbalanced HTML.
So the easiest thing to do for your list pages and your RSS feeds is to strip out the HTML first, then shorten the text to a specific length. HTML::Truncate does a nice job of this and even deals with UTF8 characters. My function does not do nearly as much however I felt like putting it up here for criticism before I delete it permanently in favor of HTML::Truncate. Also I might replace it with a HTML::Truncate procedural wrapper to make it easier for folks who don't want to write several lines of OO perl and would prefer to just call ellipsize() like I have been doing in the past. Let me know what you think.
CommentsPosted 05/27/2011 by webmaster
Worth submitting to CPAN or better to just write up how to use HTML::Truncate or better yet write a simplified wrapper for HTML::Truncate?
Posted 05/27/2011 by webmaster
Obviously you are missing the code for the strip_whitespace_scalar function. I will be posting that up in a separate document.
Technology
More About ME: |
||
Don't have a free account? Become a member!