Jump to content

Remove wraping <p></p> tags from ckeditor field.


Roych
 Share

Recommended Posts

Hello,

I know this was probably asked a million times before but I tried all kinds of "solutions found on forum" none of them works for me.
I need CkEditor but I don't want my content wraping in <p>. How can I get rid of them but keep other formating. Just get rid of first and last <p> and </p>.

I tried (works for others based on forum threads ...)

<?php 
$bodyCopy = preg_replace('/<p>\x{00A0}<\/p>/u', '', $pages->get("/nastavitve/")->kontaktni_podatki);
echo $bodyCopy; ?>

But it's not working. It still leaves the wraping <p> tags.

Thank you

R

Link to comment
Share on other sites

The regex solution you mention only strips paragraphs containing a single non-breaking space, aka. &nbsp; aka. &#x00A0;. Do you really only want to remove the surrounding <p> tags? It’s likely to become a mess if you have more than one paragraph. To get rid of all <p>s a bog-standard str_replace should do the trick, I imagine. Unless there are arbitrary attributes on it, for example.

  • Like 2
Link to comment
Share on other sites

20 minutes ago, Jan Romero said:

The regex solution you mention only strips paragraphs containing a single non-breaking space, aka. &nbsp; aka. &#x00A0;. Do you really only want to remove the surrounding <p> tags? It’s likely to become a mess if you have more than one paragraph. To get rid of all <p>s a bog-standard str_replace should do the trick, I imagine. Unless there are arbitrary attributes on it, for example.

Yes I need to remove only the surrounding <p> tags for this one textField only, because it messes my front end look. 

I'm not a coder, so not really sure how to proceed here.

Thank you very much

R

Link to comment
Share on other sites

Since these tags are introduced by CKEditor you know they’ll always be there and always look the same (I guess). So you can pretty much just count how many characters you want to skip at the beginning (3) and end (4) and do it without ever mentioning anything about <p> and </p> to PHP:

$bodyCopy = mb_substr($pages->get("/nastavitve/")->kontaktni_podatki, 3, -4);

Or you can go the replace route:

$bodyCopy = mb_ereg_replace('^<p>|</p>$', '', $pages->get("/nastavitve/")->kontaktni_podatki);

Note how using mb_ereg_replace you don’t need those annoying slashes. The regex ^<p>|</p>$ just means, “find a <p> at the beginning OR a </p> at the end”, so this leaves all the inner p-tags alone.

You can get more fancy if you need to take into account all the ways a HTML tag might appear, but eventually you’ll get into this territory.

There is also the ol’ strip_tags function and ProcessWire ships with a Textformatter module called ParagraphStripper, I believe. As well as HTMLPurifier if you really want to get your hands dirty.

  • Like 5
Link to comment
Share on other sites

5 hours ago, Jan Romero said:
$bodyCopy = mb_ereg_replace('^<p>|</p>$', '', $pages->get("/nastavitve/")->kontaktni_podatki);

 

Yes, that did the trick. Was looking everywhere for something like this.

Thank you a million ?

R

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...