Solved

Replace paragraphs in TextArea with double line break?

Posted on 2008-10-12
3
1,487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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