替换两个标签之间的内容

时间:2014-11-08 14:32:45

标签: php replace preg-replace

我想使用php替换()之间的所有内容。

我的字符串:

$string = "This is a (string)";

所需的输出是:

$string = "This is a";

我的代码不起作用:

$string = "This is a (string)";
$search = "/[^(](.*)[^)]/";
$string = preg_replace($search, "", $string);
echo $string;  // output is ")"

3 个答案:

答案 0 :(得分:3)

$result = preg_replace('/\(.+?\)/sm', 'Put your text here', $string);

试试此代码

或者将save()添加到替换

$page = preg_replace('/\(.+?\)/sm', '(Put your text here)', $page);

答案 1 :(得分:3)

这应该适合你:

<?php

    $string = "This is a (string)";
    echo preg_replace("/\([^)]+\)/","",$string);

?>

输出:

This is a

答案 2 :(得分:0)

改变一下:

$search = "/ *\(.*?\)/";