3698 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

It’s me again, same topic :confused:

I am trying to strip out the shortcodes from a given text field, but keep the content. I have a regex that works, however it doesn’t strip inner shortcodes. It only goes one level.

Here’s my code:

$output = strip_tags( preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $post->post_content ) );
$output = explode( ' ', $output, $length_callback() );
array_pop( $output );
$output = implode( ' ', $output ) . $more_callback();

When $output is this:

[test_shortcode param1="param_test"] Something inside this area [/test_shortcode]

It works well. It returns the text. However, if the content looks like this:

[test_shortcode_parent] [test_shortcode_child] Something here [/test_shortcode_child] [/test_shortcode_parent]

It returns the content between the parent shortcode. It only strips the first one.

Could anyone help me in changing the preg_replace() function in order to achieve this? It could be a good resource for anyone who has shortcodes in his theme and wants the search results to be clean.

3698 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

Here’s the entire context, because i said about search :P

$output = get_the_excerpt();

if ( empty( $output ) ) {

    // If the excerpt is empty (on pages created 100% with shortcodes), we should take the content, strip shortcodes, remove all HTML tags, then return the correct number of words

    $output = strip_tags( preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $post->post_content ) );
    $output = explode( ' ', $output, $length_callback() );
    array_pop( $output );
    $output = implode( ' ', $output ) . $more_callback();

} else {

    // Continue with the regular excerpt method

    $output = apply_filters( 'wptexturize', $output );
    $output = apply_filters( 'convert_chars', $output );

}

return $output;
481 posts
  • Has been part of the Envato Community for over 6 years
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Won a Most Wanted contest
+5 more
QBKL says

And strip_shortcodes() wouldn’t work in this case?

3698 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

And strip_shortcodes() wouldn’t work in this case?

This function strips the shortcodes, as in removing all the shortcodes from the output. So no, this would only give an empty content.. I need the plain content between the shortcodes.

3436 posts
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in United States
  • Has been part of the Envato Community for over 4 years
+1 more
organicbee says
57 posts
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
  • Has referred 50+ members
  • Has collected 1+ items on Envato Market
+2 more
nicole_89 says
http://imgur.com/1lmhoZl

Your regex seems to strip all shortcodes or is there some other problem?

9 posts
  • Sells items exclusively on Envato Market
  • Has sold $10,000+ on Envato Market
  • Has been part of the Envato Community for over 1 year
  • Has referred 10+ members
+1 more
artless says

Hmm… your example works for me:

$string1 = '[test_shortcode param1="param_test"] Something inside this area [/test_shortcode]'; $string2 = '[test_shortcode_parent] [test_shortcode_child] Something here [/test_shortcode_child] [/test_shortcode_parent]'; $output1 = strip_tags( preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $string1 ) ); $output2 = strip_tags( preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $string2 ) ); echo $output1; // Something inside this area echo $output2; // Something here
3698 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

Hey everyone

Thanks for replying, you’re right. It works with the examples. But i found the issue – it was with one parameter and a character..

If you have:

[test_shortcode_parent] [test_shortcode_child param="bad/char"] Something here [/test_shortcode_child] [/test_shortcode_parent]

It will break because of the / character. It will only filter the first shortcode..

So i have replaced the regex with:

~(?:\[/?)[^\]]+/?\]~s
and it works now :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by