2010-06-09 138 views
0

我在我自己的网站上显示wordpress内容。Preg替换Wordpress标签

然而内容具有这样的事情:

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

我将基本上要剥离的内部[]和[]本身任何这就是。

帮助非常感谢!

+0

他们都是'[caption]'还是有其他'[shortcode]'类型的内容标签? – artlung 2010-06-09 18:30:25

回答

2

你想要的正则表达式是/\[.*?\]/

<?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!" 
?> 
+0

请在'*'后加'?'(使其成为'ungreedy'), \ [[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ]'。 – Wrikken 2010-06-09 18:41:29

+0

你是对的 - 已完成修改,谢谢。 – JYelton 2010-06-09 18:51:59

1

所以像[caption id="attachment_367" align="aligncenter" width="432" caption="Version 2010!!"]代码看起来像shortcode给我。

如果你想让它什么都不做,你可以添加这个到functions.php文件在你的主题(如果你的主题没有这个文件,你需要创建它,并附在代码里<?php?>

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