我認爲你正在尋找preg_replace($pattern, $replacement, $text)
。
因此,假如您正則表達式是正確的,它似乎是工作。 (個人而言,我會做出這樣改變\[img.*\]
到\[img[^]]*\]
一些小的修改,但是這可能只是一種風格的東西)。您只需添加兩個捕獲組。
(\[img.*\].*photobucket.*)(.jpg)(\[\/img.*\])
這使您可以使用這些參考捕獲組在您更換。
$replace = "$1$2~original$3";
非常簡單 - 你只是重寫原始文本的匹配部分,以包含你想要的「〜original」。
整個代碼塊可能是這樣的:
$text = "Steering damper mounts finished - without the connecting rod the damper would get bent:
[url=http://s557.photobucket.com/user/realboss7669/media
/C85A0F5D-413D-4779-9F09-
BD43BB798238_zpse8s6dxyu.jpg.html:jaeyh8wg]
[img:jaeyh8wg]http://i557.photobucket.com/albums
/ss16/realboss7669/C85A0F5D-413D-4779-9F09
BD43BB798238_zpse8s6dxyu.jpg[/img:jaeyh8wg][/url:jaeyh8wg]";
$pattern = "/(\[img.*\].*photobucket.*)(.jpg)(\[\/img.*\])/i";
$replace = "$1$2~original$3";
$text = preg_replace($pattern, $replace, $text);
謝謝,我只是從沒想過要定義更多的組!感謝您瀏覽以及實際的PHP代碼。雖然我確實在下面使用了sln的正則表達式,但在我看來,只有2組而非3是必需的。非常感謝你們兩位!給我拉了很多頭髮。 – willdashwood