Solved

Replace paragraphs in TextArea with double line break?

Posted on 2008-10-12
3
1,476 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
Comment Utility
Change the newP to this:

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


0
 

Author Comment

by:MichaelEvangelista
Comment Utility
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
Comment Utility
** 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now