删除[]之间的文字

时间:2012-08-30 10:54:07

标签: php regex preg-replace

我有

string = 'blah blah [unwanted text] blah'

如何使用PHP返回'blah blah blah'?即我想删除方括号之间的文本。我会使用preg_replace吗?

4 个答案:

答案 0 :(得分:2)

你可以使用这个正则表达式: \[.*?\]

echo preg_replace('/\[.*?\]/', '', "blah blah [unwanted text] blah");

http://codepad.org/cNvpQOSg

答案 1 :(得分:2)

是的,您可以使用preg_replace('/\[[^]]*\]\s*/', '', $your_string)

答案 2 :(得分:1)

完整的解决方案:

$input = preg_replace('/\[[^\]]*\]\W*/i', '', $input);

答案 3 :(得分:0)

<?php
$string = 'blah blah [unwanted text] blah';
$x=explode("[",$string);
$y=explode("]",$string);
echo $x[0].$y[1];
?>

- 使用它以便您可以按预期获得输出。