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.

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);

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.

.. er... hang on... this solution won't work because I need to retain paragraph tags which are not within the table.

 

I want to strip out paragraph tags, but only those ones within td tags (which may or may not themselves contain attributes)...

 

...?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.