Help Please to Remove extra <P> from textarea form field coldfusion 11

Extra <P> causing problems from user generated page.  I would like to remove the superfluous occurances of <P> Example below as extra <P>.    I can do this on the server side with coldfusion.  I use ckeditor as a recent addition. Could be old text from previous system, or copied in by user - not sure.

But want to remove extra <P> . I will do this after the user submits, and before adding to the database, and do it on #form.notes#  So, Please,  how can I go through this string and remove all <P> where there are more then one  <P>  after </P> and before next </P>

So only end up with   </p> <p> </p>
not </p> <p> <p> </p>
or </p> <p> <p> <p> </p>
and so on...

Example generated text from user below, that needs cleaning up.

</textarea>
<p><strong>No arrest record.</strong></p>    <P>
    <P>
<p><strong>No drinking or drug habits.</strong></p>    <P>
    <P>
<p><strong>No drama in my life.</strong></p>    <P>
    <P>
<p><strong>And I gave up smoking in 1995.</strong></p>    <P>
    <P>
    <P>
    <P>
<p>Generally I will gladly help pet owners/parents in <strong>all 50 states of the US</strong>, but being based in Central PA myself, I will need my expenses*&nbsp;to be covered (trains, buses, air fare, food). No additional fees or charges apply.</p>    <P>
    <P>
    <P>
    <P>
<p><strong>*Exceptions:</strong></p>    <P>
    <P>
    <P>
    <P>
<p><strong>Totally free for pets / apartments / homes in&nbsp;New York City, NYC, Brooklyn, Manhattan, Philadelphia Center City,&nbsp;Washington DC, Georgetown, Alexandria, Baltimore</strong></p> <P>
</textarea>
												

Open in new window

Ian WhiteOwner and FounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
You can put the "burden" of cleaning that up to the browser.  See example below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">
function doValidation(form)
{
	clean(form.notes);
return true;
}

function clean(e)
{
	var matches = e.value.match(/<\/p>(\s*<p>)+/ig);
	if( matches)
	{
		var str = e.value;
		for(var i=0,limit=matches.length-1; i<limit;++i)
		{
			str = str.replace(matches[i],"</p>\n\n<p>");
		}
		str = str.replace(matches[i],"</p>");
	}
	e.value=str;
	
	//uncomment the following to see the new value of the textarea
	//alert(e.value);
	
return e.value;
}
</script>
<form action="#" method="post" onsubmit="return doValidation(this)">
<textarea id="notes" name="notes" rows="30" cols="80">
<p><strong>No arrest record.</strong></p>    <P>
    <P>
<p><strong>No drinking or drug habits.</strong></p>    <P>
    <P>
<p><strong>No drama in my life.</strong></p>    <P>
    <P>
<p><strong>And I gave up smoking in 1995.</strong></p>    <P>
    <P>
    <P>
    <P>
<p>Generally I will gladly help pet owners/parents in <strong>all 50 states of the US</strong>, but being based in Central PA myself, I will need my expenses*&nbsp;to be covered (trains, buses, air fare, food). No additional fees or charges apply.</p>    <P>
    <P>
    <P>
    <P>
<p><strong>*Exceptions:</strong></p>    <P>
    <P>
    <P>
    <P>
<p><strong>Totally free for pets / apartments / homes in&nbsp;New York City, NYC, Brooklyn, Manhattan, Philadelphia Center City,&nbsp;Washington DC, Georgetown, Alexandria, Baltimore</strong></p> <P>
</textarea>
<input type="submit"/>
</form>												
</body>
</html>

Open in new window

0
_agx_Commented:
(no points... just tweaked hielo's suggestion for CF)

You should back that up with similar handling on the server side. Note, neither the original expression (or the code below) remove the last trailing "<p>".

     <cfset newText = REReplaceNoCase(form.notes, "</p>(\s*<p>)+", "<p>1", "all")>

As an aside, parsing html with regex's is notoriously problematic. If you only ever need to replace consecutive <p> tags,  a regex should do the trick. For anything beyond that, you are better off with a more robust tool like JSoup which was designed specifically for html parsing.
0
Ian WhiteOwner and FounderAuthor Commented:
Sorry the 2nd solution got rid of the </p> too . I will try the first one.

<p><p>-Reliable, responsible, and I take care of your home and your furry loved ones as if they were my own.<p>1-I love animals and plants. And house-pet sitting is my preferred means of travel.<p>1-Thirty+ years of experience as a home, pet and plant sitter.<p>1-Self-employed professional with several graduate degrees in the social sciences.<p>1-Dual citizen of the USA and Argentina (US born), with home bases in Austin Texas and Buenos Aires.<p>1-Bi-lingual in English/Spanish, and I &quot;get by&quot; in four additional languages.<p>1-Intuitive bond/connection with most animals. I get along great with dogs, but I have a special connection with cats. My friends claim I&#39;m something of a &quot;cat whisperer.&quot; ;)<p>1-Experience with elderly and special needs animals.<p>
0
_agx_Commented:
Yes, you're right.  I was testing the code on trycf.com and pasted the wrong example.  Should have posted a link ;-) Here's what it should have been.

<cfset form.notes= REReplaceNoCase(form.notes, "</p>(\s*<p>)+", "</p><p>", "all")>
<cfoutput>#HTMLEditFormat(form.notes)#</cfoutput>

Open in new window


Edit:   Note, you still need to do something about the last trailing "<p>" (if it really exists in your actual string).   You could do the replacements within a loop (like in the JS code) but I'm guessing there's a simpler option in CF.  I'll leave that to a regex guru, since regex's aren't my forte.

BTW, you can still use the client side code too, but if it's important - don't depend on client side stuff alone.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ian WhiteOwner and FounderAuthor Commented:
Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.