?
Solved

spliting a paragraph into two sections

Posted on 2011-10-08
4
Medium Priority
?
223 Views
Last Modified: 2012-05-12
Hi,
I am facing a problem in my coldfusion8 application. I need to divide the text in 2 paragraphs, but somehow the words are cutting in the middle of it. For eg: the word READABLE cuts in and left in first paragraph as "REA" and in second paragraph it starts from "DABLE". I am looking for a solution which stops on space or period so user can read it easily.

The sample code is as under:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>

<body>

<cfset stText = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose."/>

<p id="Page1">
<h1>PARAGRAPH 1</h1>
<cfoutput>
<!---para 1--->
  <cfif IsDefined("VARIABLES.stText") AND VARIABLES.stText NEQ "">
	<cfset stLengthA = Len(VARIABLES.stText) />
	  <cfif VARIABLES.stLengthA LTE 30>
      	#RTrim(VARIABLES.stText)#
      <cfelse>
      	<cfset stDivideA=VARIABLES.stLengthA/2 />
      	<cfset stPrintA = Round(VARIABLES.stDivideA) />
      	#RTrim(left(VARIABLES.stText, stPrintA))# &nbsp;&nbsp; (PARAGRAPH 1, continued on para 2)
      </cfif>
  </cfif>
</p>
<p id="Page2"> 
<!---para 2--->
<h1>PARAGRAPH 2</h1>
  <cfif IsDefined("VARIABLES.stText") AND VARIABLES.stText NEQ "">
	<cfset stLengthB = Len(VARIABLES.stText) />
    <cfif VARIABLES.stLengthB GT 30>
    	<cfset stDivideB=VARIABLES.stLengthB/2 />
    	<cfset stPrintB = Round(VARIABLES.stDivideB) />
    	#LTrim(Right(VARIABLES.stText, (stPrintB)+12))# 
<!---(Using +12 coss text is cutting in between words for eg: (THE WORD "readable", cuts and print ONLY "rea" in first paragraph , so atleast user can read what was the whole sentence when they read in next paragraph, instead of reading from "dable" and most of the time)--->
    </cfif>
  </cfif>
</cfoutput>
</p>
</body>
</html>

Open in new window


The result of the above code as under:

PARAGRAPH 1
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like rea    (PARAGRAPH 1, continued on para 2)

PARAGRAPH 2
look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose.

Any help, suggestion, or feedback would be deeply appreciated. Thanks in advance....
0
Comment
Question by:s_hausen
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 36937408
Find the middles of the string (ie total characters / 2), then find the first period or space right before that. Obviously add validation if needed.

<cfset stText = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose."/>

<!--- split the string in half --->
<cfset midPoint = ceiling(len(stText) / 2)>
<!--- find the first space or period from the end --->
<cfset splitAt   = reFind("[[:space:]\.][^[:space:]\.]*$", left(stText, midPoint ))>

<cfoutput>
midPoint = #midPoint#<br>	
splitAt = #splitAt#<br>	
<strong>Paragraph1:</strong> #mid(stText, 1, splitAt)#<hr>
<strong>Paragraph2:</strong> #right(stText, len(stText)-splitAt)#<hr>
</cfoutput>

Open in new window

0
 

Author Comment

by:s_hausen
ID: 36937530
hi agx,
one last thing..if i'm using # pound symbol or " double quote in my text its giving error. I tried, replace, replacelist and HTMLEditFormat functions but none of'em work, any clue???

<cfset stText = "It is a long established # fact that a reader will be "distracted" by the readable content of a page when looking at its layout. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for."/>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36937551
It's easier to use cfsavecontent instead of cfset. It captures all content as is. Then neither one is a problem.  

<cfsavecontent variable="stText">It is a long established # fact that a reader will be "distracted" by the readable content of a page when looking at its layout. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for.</cfsavecontent>

Just be aware cfsavecontent honors allspaces. So if you write it this way for readability, you'll need to use trim() to remove the leading and trailing white space

<cfsavecontent variable="stText">
It is a long established # fact that a reader will be "distracted" by the readable content of a page when looking at its layout. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for.
</cfsavecontent>

Open in new window


If you prefer to use <cfset> you'll have to escape them ie use 2 double quotes or 2 ##'s.  Kind of ugly, but that's what you have to do ;)

<cfset stText = "It is a long established ## fact that a reader will be ""distracted"" by the readable content of a page when looking at its layout. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for.">

Open in new window

0
 

Author Closing Comment

by:s_hausen
ID: 36937556
i'm very impressed by this expert knowledge.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question