Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to insert new line into text variable

    Ok guys, heres the quick run down...
    Im converting HTML into plain text. The HTML is contained within a variable, and is passed to a function cleanHTML for removal of the tags. What I need to do is detect line breaks e.g <Br> <P> </P>, and then insert a new line character.
    a is the location of the first '<'
    c is the text leading up to the first '<'
    temp is the contents within the '<' and '>' of the HTML which is checked by
    if (temp.toLowerCase() == ('br' || 'p' || '/p'))
    If these characters are found a new line should be added to the end of c by,
    c = (c + ************** ); //insert new line
    the word is then combined and rechecked then returned.
    Everything works fine except the newline piece, I can successfully add text such as '_______' and get exactly what you'd expect.
    I have tried the following...
    '/n' "/n" charCode(013) CharCodeToString(013)
    and am out of ideas. Any help would b greatly appreciated.



    function cleanHTML(HTMLWord)
    {
    a = HTMLWord.indexOf("<");
    b = HTMLWord.indexOf(">");
    HTMLlen = HTMLWord.length;

    c = HTMLWord.substring(0, a);
    if(b == -1)
    b = a;

    temp = HTMLWord.substring((a+1),(b));
    if (temp.toLowerCase() == ('br' || 'p' || '/p'))
    c = (c + *********); //insert new line

    d = HTMLWord.substring((b + 1), HTMLlen);
    Word = c + d;

    tmp = Word.indexOf("<");
    if(tmp != -1)
    Word = cleanHTML(Word); // recheck

    return Word;
    }

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Something like this mate? Need to pass in the input element object and the output element object into the cleanHTML function.
    Code:
    function cleanHTML(objIn, objOut) {
    	objOut.value = objIn.value.replace(/<[^>]*>/g, function tagMatch(s) {
    			if(s.indexOf("<img") > -1) { // leave images alone!
    				return s;
    			} else if(s.indexOf("/") > -1) { // add a newline after closing tags
    				return "\n";
    			} else {
    				return ""; // clean out all opening tags
    			}
    		}	
    	);
    }
    Example usage:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function cleanHTML(objIn, objOut) {
    	objOut.value = objIn.value.replace(/<[^>]*>/g, function tagMatch(s) {
    			if(s.indexOf("<img") > -1) { // leave images alone!
    				return s;
    			} else if(s.indexOf("/") > -1) { // add a newline after closing tags
    				return "\n";
    			} else {
    				return ""; // clean out all opening tags
    			}
    		}	
    	);
    }
    </script>
    </head>
    
    <body>
    <form name="frm" onsubmit="cleanHTML(this.txtInput, this.txtOutput);return false;">
    <textarea name="txtInput" cols="100" rows="10">
    <p>Hello world</p>
    <h3>line 2!</h3>
    <br>
    Line 3!
    <img src="img.gif">
    </textarea>
    <br><br>
    <textarea name="txtOutput" cols="100" rows="10"></textarea>
    <br><br>
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>
    Hope this helps.

    m_n
    Last edited by martin_narg; 08-12-2005 at 06:48 PM.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •