Removing extra p tags

Hello All,

When I paste text from MS word document, it creates extra p tags.
Where in the code I can modify it so it only shows one opening and closing paragraph?

Here is a link to test: https://codepen.io/anon/pen/hFAdk

Output text:

"<p>Mauris adipiscing lacus sit amet libero dapibus lobortis.</p>
<p></p>
<p>Phasellus iaculis iaculis urna ut ullamcorper. Nullam hendrerit lectus non orci volutpat id pulvinar mi dictum. </p>
<p></p>
<p>Pellentesque felis quam, posuere in faucibus at, pharetra et quam. Pellentesque habitant morbi tristique senectus et. </p>"

Thank you very much for your help :slight_smile:

Hi @mp5163093 I’m not exactly sure what’s the problem but you seem to have a bunch of regular expressions replacing content in your JavaScript. Well my best guess is one of those is breaking your HTML and producing all of those p tags.

Generally that’s not recommended, although it’s not clear how you are copying the text and where you are pasting it. Word creates awful HTML, so the first recommendation is to use a code editor like Atom instead. Even if you do this as an intermediary step.

Are you pasting this into WordPress or something? Seems every line return is being treated as a new paragraph. That’s party why I think pasting the text into a plaiin text editor first might help, but we need a better idea of the conditions you’re working in.

Hey Ralphm,
Is for a small cms and the code as it is works great, the only thing is that is creating those extra paragraphs tags. :frowning:
I can’t use Atom editor, or wordpress.
As you can see my example text it does show how extra p tags gets created. The cool thing is that the script cleans up all of the MS word html code.
I believe the problem is in the regular expressions. by the way Andres thank you for your input.
To test the code might want to copy and paste any text from MS word in to the codepen

Thank you very much for your quick reply Ralphm

Drop it into Notepad or similar, then cut & paste it back out, then put the tags back in that you need. It might take a little time, but it’ll definitely help clean things back up.

I could do that but users won’t do it :frowning:

Ah, didn’t realise they were the ones doing it. That doesn’t help.

You’re welcome.
I don’t hope to despair you but your problem reminded me to this funny article :

And it actually must be the case that to build a WYSIWYG editor you do need regular expressions, but that must be the reason also why they are so hard to build.
I wish you good luck solving your problem

1 Like

If the CMS is using php try using $text = str_replace('<p></p>', '', $text);

1 Like

Often you’ll find the wysiwyg editors in CMSs have settings for how the editor should deal with paragraph returns etc. Do you know which editor the CMS is using?

1 Like

Hi John,

Yes I could use PHP
In which file should i add your line of code ?
Should I create a new include php file ?
How can I can call the line of code in the form ?

https://codepen.io/anon/pen/hFAdk

Thanks you for your help :slight_smile:

Good Morning Ralphm,

This is a very simple cms and an editor can have way too many functions. I only need the cms to do basics.
Thanks

I looked at the script and unfortunately there is no standard form or textarea being used so it makes it difficult to incorporate PHP with JavaScript.

It is possible but I do not know how to convert and replace var content = $(this).html();

Perhaps some JavaScript guru could supply a solution?

Hey John,

Thank you man for trying and let’s wait for someone to rescue us :wink:

1 Like

I got the regular expression working but when i added to the other source code, does nothing :frowning:

<p[^>]*>[\s|&nbsp;]*<\/p>

Running code:

Does anyone see why is not working ?

Thanks in advance for your time

First paste the text in notepad and remove all other unwanted p tags .you should use the tag in beginning and close it at the end of sentence.

Hey John, my friend Robert figure out the problem and it was the regular expression http://booden.net/ContentCleaner.aspx <[^/>][^>]>\s</[^>]+> Thanks for your help everyone :slight_smile:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.