views:

68

answers:

2

Hello,

I'm displaying wordpress content on my own site.

However the content has such things as:

[caption id="attachment_367" align="aligncenter" width="432" caption="Version 2010!!"]

I would basically like to strip anything thats inside [] and the [] themselves.

Help greatly appreciated!

+2  A: 

The regular expression you want is /\[.*?\]/

<?php
$old_content = 'Hello [caption id="attachment_367" align="aligncenter" width="432" caption="Version 2010!!"] World!';
$new_content = preg_replace('/\[.*?\]/', '', $old_content);
echo $new_content; // result: "Hello World!"
?>
JYelton
Please add a '?' after the '*' (make it ungreedy), or make it `\\[[^\\]]*\\]` (anything but ']'), otherwise, this string will fail to show 'this valid content should stay' : `[ to remove ] this valid content should stay [another remove]`.
Wrikken
You're correct - modifications done. Thanks.
JYelton
A: 

So code like [caption id="attachment_367" align="aligncenter" width="432" caption="Version 2010!!"] looks like a shortcode to me.

If you want it to do nothing, you could add this to the functions.php file in your theme (if your theme does not have that file, you would need to create it and enclose this code inside <?php and ?>:

function do_nothing_caption() {
  return '';
}
add_shortcode('caption', 'do_nothing_caption');
artlung