Expression Engine 2 Plugin: Empty P Stripper

Feature image

I’ve got a small little Expression Engine add on., but it might be helpful if you use Pixel and Tonic’s WYGWAM to give your client’s text entry into an Expression Engine textfield a little UX love.

WYGWAM’s a good tool that I’ve used several times on sites. Clients find it an easy thing to pick up. For a recent build though, I kept noticing some strange spacing when my client was using it to create copy. There were empty P tag pairs scattered throughout the copy. I tried tweaking the preferences for CKEditor, which is the engine behind the scenes of WYGWAM, but couldn’t get these empty P tags from happening.

I ended up making a little plug in for the site to solve the problem I was having. I called it Empty P Stripper and is available on GitHub. It’s free and worth every penny. ;-)

(If you’re wondering, yes, it’s a code tweak from my SuperGeekery Tag Stripper. It was different enough that I didn’t want to add clutter to it though.)

Once you’ve got it in your /system/expressionengine/thirdparty/ folder, you can use it like this:

{exp:emptypstripper:stripMyPsPlease}
{wygwym_generated_content}
{/exp:emptypstripper:stripMyPsPlease}

It will turn something like this:

<p>This is a paragraph of text.</p> 
<hr>
<p> &nbsp; </p>

And return this:

<p>This is a paragraph of text.</p> 
<hr>

Good luck. Let me know if it works for you.

Comments on this post.

Looks great. CKE can be a pain (those extra tabs!!). So, what’s the advantage of this plugin, over using your TagStripper?

Be great not to have to use a tag in your template at ALL. =)

Thanks for your work, it’s very helpful.

By Allan White on Apr 09 2012

TagStripper will strip the tags, but leave the content intact between the tags. The EmptyPStripper wipes out the content between empty P tags as well, so you don’t end up with random non-breaking spaces. As soon as you asked this question, I realized I should have probably just modified TagStripper to could nonbreaking spaces as empty content as well when it did it’s job. Live and learn, right?

I’m glad you found it helpful. It’s good to hear feedback like that!

By John Morton on Apr 09 2012

I see. I reread your post, and noted you described the difference between this and TagStripper.

That might make sense, based on your audience: people who care about the extra “p’s” are likely interested in TagStripper also. Now I just have to learn regex to get the most out of it…

Enjoyed the recent post re: add-on development. Great stuff.

By Allan White on Apr 09 2012

Hi Allen,

Regex is useful stuff. Check out http://tutsplus.com/course/regular-expressions-up-and-running/. It’s not a free course, but definitely worth checking out.

By John Morton on Apr 09 2012

Hi John, thanks for this. Helpful and a great intro for me as an ee newb to a simple ee plugin.

I think the regex might might need a little tweaking.

$patterns = ‘{]*>*>[\\n\\r\\s ]*<\\/p[^>]*>}’;

if &nbsp; is in brackets that means that an single of the characters &, n, b, s, p, ; will match.

so n would match your regular expression and be replaced.

so would nb etc.

I tried this but it did not work

$patterns = ‘{]*>*>[\\n\\r\\s]*( )?[\\n\\r\\s]<\\/p[^>]*>}’;

By Alex on Jun 08 2013

Sounds right, Alex. Good catch!

To isolate the “&nbsp” to be required as a whole word I think I’d need to do this:

<p[^>]*>*>[\n\s]*(&nbsp;)*[\n\s]*<\/p[^>]*>

That needs to be escaped though to work in the PHP code which means it would probably look more like:

$patterns = '{<p[^>]*>*>[\\n\\s]*(&nbsp;)*[\\n\\s]*<\\/p[^>]*>}';

I will update the project on Github. Thanks!

By John Morton on Jun 08 2013

Leave Your Comment:

name:

email:

location:

url:

your comment:

Remember my personal information

Notify me of follow-up comments?

Twitter Feed

John Morton talking on Twitter




Meanwhile on Instagram… //

My latest shot from Instagram.
#nyc