2013-12-21 22 views
0

我需要改變這一行:count(id)變化的內容外而內支架與了preg_replace

排隊這樣count(tableName. ID )

我嘗試用預浸比賽要做到這一點並替換這樣的:

$a = "count(id)"; 
    $regex = "/\w{3,}+\W/"; 
    $dd = preg_match("/\(.*?\)/", $a, $matches); 
    $group = $matches[0]; 

    if (preg_match($regex, $a)) { 

    $c = preg_replace("$group", "(table.`$group`)", $a); 

    var_dump($c); 

    } 

我得到的輸出是:count((table.(id)))它輸出我額外的括號。我知道這個問題,但我找不到解決方案,因爲我的正則表達式知識不太好。

回答

1
$a = "count(id)"; 
$regex = "/\w{3,}+\W/"; 
$dd = preg_match("/\((.*?)\)/", $a, $matches); 

$group = $matches[1]; // <-- you'll get error if the above regex doesn't match! 

if (preg_match($regex, $a)) { 
    $c = preg_replace("/$group/", "table.$group", $a); 
} 
+0

感謝您的幫助! – viktorino

+0

是否爲您做了這項工作? –

+0

實際上,我發現自己寫的模式,但你的是相同的))是的謝謝。 – viktorino