3804 posts
  • Elite Author
  • Author Level 11
  • Trendsetter
  • 7 Years of Membership
+12 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.

3804 posts
  • Elite Author
  • Author Level 11
  • Trendsetter
  • 7 Years of Membership
+12 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;
554 posts
  • Bundle Boss
  • Most Wanted Winner
  • Author Level 5
  • 6 Years of Membership
+4 more
QBKL
says

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

3804 posts
  • Elite Author
  • Author Level 11
  • Trendsetter
  • 7 Years of Membership
+12 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.

3439 posts
  • Author Level 4
  • Collector Level 2
  • Community Superstar
  • United States
+2 more
organicbee
says
60 posts
  • Exclusive Author
  • 3 Years of Membership
  • Affiliate Level 3
  • Collector Level 1
+2 more
nicole_89
says
http://imgur.com/1lmhoZl

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

9 posts
  • Exclusive Author
  • Author Level 5
  • 2 Years of Membership
  • Affiliate Level 2
+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
3804 posts
  • Elite Author
  • Author Level 11
  • Trendsetter
  • 7 Years of Membership
+12 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 :)
by
by
by
by
by
by