Solved

Replace paragraphs in TextArea with double line break?

Posted on 2008-10-12
3
1,482 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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