Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace paragraphs in TextArea with double line break?

Posted on 2008-10-12
3
Medium Priority
?
1,503 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
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 …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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