...

View Full Version : how to insert new line into text variable



ywfbi
08-12-2005, 01:51 PM
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')) :thumbsup:
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. :thumbsup:
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 :confused:

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

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

return Word;
}

martin_narg
08-12-2005, 06:42 PM
Something like this mate? Need to pass in the input element object and the output element object into the cleanHTML function.


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:


<!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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum