Regular expression to remove <p> tags around elements wrapped in [...]'s

I'm a total regexp noob. I'm working with wordpress and I'm desperately trying to deal with wordpress's wautop, which I hate and love (more hate!). Anyways I'm trying to remove <p> tags around certain commands.

Here's what I get:

<p>
[hide]
<img.../>
[/hide]
</p>

or

<p>
[imagelist]
<img .../>
<img .../>
[/imagelist]
</p>

Here's what I'd like:

[hide]
<img.../>
[/hide]

or

[imagelist]
<img .../>
<img .../>
[/imagelist]

I've tried:

preg_replace('/<p[^>]*>(\[[^>]*\])<\/p[^>]*>/', '$1', $content); // No luck!

EDIT: When I am doing the regexp it is still just a variable containing text.. It is not parsed as html yet. I know it is possible because I already did it with getting rid of p tags around an image tag. So I just need a regexp to handle text that will be parsed as html at some point in the future. Here's a similar question

Thanks! Matt Mueller

Solutions

The language of matching HTML tags is context-free, not regular. This means regular expressions are probably not the right tool to use here. Context-free languages require parsers rather than regular expressions. So, you can either remove ALL <p> and </p> tags with a regular expression, or you can use an HTML parser to remove matching tags from certain parts of your document.

You can't use regular expressions to parse HTML, because HTML is, by definition, a non-regular language. Period, end of discussion.

Try this regex:

'%<p[^>]*>\s*(\[([^\[\]]+)\].*?\[/\2\])\s*</p>%s'

Explanation:

\[([^\[\]]+)\] matches the opening bbcode tag and captures the tag name in group #2.

\[/\2\] matches a corresponding losing tag.

.*? matches anything, reluctantly. Thanks to the s flag at the end, it also matches newlines. The effect of the reluctant .*? is that it stops matching the first time it finds a closing bbcode tag with the right name. If tags are nested (within tags with the same name) or improperly balanced, it won't work correctly . I wouldn't expect that be a problem, but I have no experience with WordPress, so YMMV.

Similar questions

Add class to div wrapped around custom post type based off posts category
I'm looking for a way to inject a class into a div wrapped around a custom post type based off the category that post is attributed to.There will be three categories; ebook (class name cat1) infographic (class name cat2) case study (class name cat3) Here is my code so far to call the custom post type. Currently, the page displays all posts as the l...
I want every four elements in WP While Loop wrapped in a Div Container (Index.php) Wordpress
I want Every Four Cfcolumns Wrapped in One Div Container having a Class here is my While Loop in Index Page:
How to remove p tags around img and iframe tags in the acf wysiwyg field
I am querying an advanced custom fields wysiwyg field. Problem is in the output wordpress wraps occasional imgs and iframes with p tags no matter what i try. Deactivate the wpautop works remove_filter ('acf_the_content', 'wpautop');but if i try to exclude only imgs and iframes with the following snippet it fails: Does anyone have a suggestion how t...
Remove p tags from around object tags in WordPress
I'm looking for a way to remove p tags from around object tags in WordPress. I have already found a way to remove the p tags from around img and iframe tags, but would like to extend the following code to include the object tag. Any help would be greatly appreciated. Thanks in advance.
Use Regular Expression to get tag from permalink url during wp_rewrite in generate_rewrite_rules
I'm messing with wp_rewrite and have spent the past few hours trying to figure regular expressions out again (i seem to have forgotten.) This is probably extremely easy for someone. Basically I'd like to match an alpha expression and stop at teh first occurance of a forward slash. Here's what i'm doing: I have a url http://www.mydomain.com/basename...
404 regular-expression advice needed
I have been working hard trying to understand regular-expressions to use in my web site using the redirection plugin. I have changed my permalinks a few times in the past year because I did not know better or had incorrect advice. The upshot of this is that Google web master tools shows hundreds of 404's. I decided to tackle fixing the 404 issue wi...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .