3430 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 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.

3430 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 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;
322 posts
  • Envato Studio (Microlancer) Beta Tester
  • Most Wanted Bounty Winner
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Romania
QBKL says

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

3430 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 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.

3192 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
  • Exclusive Author
organicbee says
35 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 10 and 49 users
  • Sold between 50 000 and 100 000 dollars
nicole_89 says
http://imgur.com/1lmhoZl

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

3 posts
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Bought between 10 and 49 items
  • Has been a member for 1-2 years
  • Europe
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
3430 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 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