Solved

Replace paragraphs in TextArea with double line break?

Posted on 2008-10-12
3
1,486 Views
Last Modified: 2012-05-05
I have a function, gleaned from an earlier ExEx thread, that takes html code from a database and shows it in plain-text format in a textarea.

Everything is working fine, except I want to have new paragraphs in the html replaced by *double* line returns in my text version .
I've spent forever trying various character combinations, and I can't get double-spacing to take hold.

Here is the function

function htmlToText(theHtml)
{  newP = chr(10) & chr(13) & chr(10) & chr(13);
   theText = REReplaceNoCase(theHTML,"<a .*(href=['""]?)([^'"" ]+)['"" ][^>]+>([^<]+)</a>","\3: \2","all");
   theText = REReplaceNoCase(theText,"<p[^>]*>",newP,"all");
   theText = REReplaceNoCase(theText,"<[^>]+>","","all");
   return theText;
}

As you can see I am trying to force a double line break, but the textarea still interprets this as a single line return.

If I insert any character between the two line returns, it gives me a new line, with the character, then another new line, as it should.
{  newP = chr(10) & chr(13) & '.' & chr(10) & chr(13);
but if I just have the two sets of line return characters, I only get one new line.

how can I force a double line replacement in my textarea?

I tried using a whitespace character like a 'space' between the lines, but that doesn't do anything, that i can tell.


0
Comment
Question by:MichaelEvangelista
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 22698914
Change the newP to this:

newP = chr(13) & chr(13);


0
 

Author Comment

by:MichaelEvangelista
ID: 22699131
boy do i feel dumb. Turns out another function, which I have in place to remove whitespace from my code before outputting to the browser, is responsible for stripping out the double line breaks. Bypassing the function gives me what I have been looking for, using the original code I posted.

your answer is correct but so is what i started with!

Maybe I can change the course of this just a little?

I'd like to modify my html compress function to ignore any code between <pre> and </pre>
(For the most part I run this with the level set to "2" - using "1" gives the same result as far (as double line spacing is concerned.)

I am not a regex wiz.
How can I modify this code so that before doing anything else it will ignore any content between the tags <pre> and </pre> ?





<!--- htmlCompress cf function --->
<cfscript>
/**
 * Replaces a huge amount of unnecessary whitespace from your HTML code.
 * @param sInput 	 HTML you wish to compress. (Required)
 * @return Returns a string. 
 * @author Jordan Clark (JordanClark@Telus.net) 
 */
function HtmlCompressFormat(sInput)
{
   var level = 2;
   if( arrayLen( arguments ) GTE 2 AND isNumeric(arguments[2]))
   {
      level = arguments[2];
   }
   // just take off the useless stuff
   sInput = trim(sInput);
   switch(level)
   {
      case "3":
      {
         //   extra compression can screw up a few little pieces of HTML, doh         
         sInput = reReplace( sInput, "[[:space:]]{2,}", " ", "all" );
         sInput = replace( sInput, "> <", "><", "all" );
         sInput = reReplace( sInput, "<!--[^>]+>", "", "all" );
         break;
      }
      case "2":
      {
         sInput = reReplace( sInput, "[[:space:]]{2,}", chr( 13 ), "all" );
         break;
      }
      case "1":
      {
         // only compresses after a line break
         sInput = reReplace( sInput, "(" & chr( 10 ) & "|" & chr( 13 ) & ")+[[:space:]]{2,}", chr( 13 ), "all" );
         break;
      }
   }
   return sInput;
}
</cfscript>

Open in new window

0
 

Author Comment

by:MichaelEvangelista
ID: 22699160
** CORRECTION **

I just started playing with that function above.
Using the (1) setting for the replacement level made no difference...
UNTIL I replaced my code with Zvonko's.

Why is
{  newP = chr(13) & chr(13);

different than

{  newP = chr(13) & chr(10) & chr(13) & chr(10);

?

I will accept points on the original question. If you have a quick answer for the functin above, great, if not I will post as new question when I need it. For now I can use the (1) method on pages that need lighter compression (skipping the function altogether caused me a different set of problems)

thank you zvonko!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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