Jump to content

Regex To Strip Paragraph Tags Out Of Table Cells


peppericious

Recommended Posts

I'm using tinyMCE for textareas in a form.

 

Some users of the form are copy/pasting content from Word. I've managed to get fairly clean content using the Paste tinyMCE plugin.

 

However, in the case of tables, cell content is being pasted like this:

<td valign="top" width="148"><p>text</p></td>

 

How can I strip out those paragraph tags so that I simply get this:

<td valign="top" width="148">text</td>

 

Thanks in advance.

Link to comment
Share on other sites

If you are certain it will always be a simple paragraph tag (no attributes) then you can use str_ireplace, as it is technically more efficient.

 

 

$content =str_ireplace(array('<p>','</p>'),'',$content);

 

If it may possibly have attributes, you can use preg_replace instead.

 

$content = preg_replace('~</?p[^>]*>~i','',$content);

Link to comment
Share on other sites

If you are certain it will always be a simple paragraph tag (no attributes) then you can use str_ireplace, as it is technically more efficient.

 

 

$content =str_ireplace(array('<p>','</p>'),'',$content);

 

If it may possibly have attributes, you can use preg_replace instead.

 

$content = preg_replace('~</?p[^>]*>~i','',$content);

 

Very, very helpful. Thank you.

Link to comment
Share on other sites

This RegExp should do it:

#(<td[^>]*>)(?:<p>(.*?)</p>)*(</td>)#

 

That said though, what you really should do is use DOMdocument or some other manner of properly traversing the DOM object. Regular Exp<b></b>ressions are for regular languages, not markup and other irregular languages.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.