2014-11-08 74 views
1

我想用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
$result = preg_replace('/\(.+?\)/sm', 'Put your text here', $string); 

試試這個代碼

或保存()將它們添加到更換

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

這應該爲你工作:

<?php 

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

?> 

輸出:

This is a 
0

只是改變:

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