2012-09-18 37 views
2

我有幾個在文本文檔中,這些標籤與很多其他的內容,以及(存儲在$內容):PHP的preg_replace certrain串

[[tag:author|id:6329]] 

而且我一直在試圖取代所有這些帶有給定id的標籤(在本例中爲6329)和另一個字符串。

$id = 6329; 
$replacement = $this->format($id); 
$pattern = "/(\[\[tag:author|id:".$id."\]\])/"; 
preg_replace($pattern, $replacement, $content); 

儘管這看起來不起作用,並且在過去的幾個小時裏我一直在撕掉我的頭髮。任何人都可以看到錯誤?非常感謝。

回答

1

您也需要轉義|,不需要用()包裝它。

嘗試:

$pattern = "/\[\[tag:author\|id:".$id."\]\]/"; 
+0

你是真棒。 – frigg