我有這樣的文字:PHP的preg_replace [於]
$t = [[Image:United Kingdom images gbr image031 jpg.png|141px|link=HomePage|United Kingdom images gbr image031 jpg.png]]
[[Image:United Kingdom images gbr image030 jpg.png|141px|United Kingdom images gbr image030 jpg.png]]
[[Image:United Kingdom images gbr image031 jpg.png|141px|link=Contact|United Kingdom images gbr image031 jpg.png]];
,我想將[noinclude]之前有文本| [和之間的鏈路=] ]和]]後添加[/ noinclude]。所以我想將它添加到第一個和第三個圖像。當我嘗試像這樣:
preg_replace('/\[\[Image:(.*?)\]\]/', '[noinclude][[Image:$1]][/noinclude]', $t);
它包裝所有3張圖片。當我嘗試這樣的:
preg_replace('/\[\[Image:(.*?)\|link=(.*?)\]\]/', '[noinclude][[Image:$1|link=$2]][/noinclude]', $t);
結果是:
[noinclude][[File:United Kingdom images gbr image031 jpg.png|141px|link=ADI|United Kingdom images gbr image031 jpg.png]][/noinclude]
[noinclude][[File:United Kingdom images gbr image031 jpg.png|141px|United Kingdom images gbr image031 jpg.png]]
[[Image:United Kingdom images gbr image031 jpg.png|141px|link=ADI|United Kingdom images gbr image031 jpg.png]][/noinclude]
所以它正確地增加了我的標籤,第一個,但是當它到達第二圖像,它不停在第二圖像的],但繼續第三圖像的](因爲它是尋找|鏈接)
我怎麼能強制其停在],如果有|友情鏈接= [[]]之間?
謝謝!
更新: 而如果文本更復雜?就像這樣:
$t = '<span class="United Kingdomtemplate_colour">Additional remarks</span><br> <br> Green sign with white and yellow text with blue plate with white text displaying that the indicated direction(s) lead to a motorway. [[Image:United Kingdom images gbr image031 jpg.png|141px|link=Home Page|United Kingdom images gbr image031 jpg.png]]<br>White sign with black text with blue plate with white text displaying that the indicated direction(s) lead to a motorway. [[Image:United Kingdom images gbr image046 jpg.png|292px|United Kingdom images gbr image046 jpg.png]]<br> <br> <span style="vertical-align: top;">White sign with black text with blue plate with white text displaying that the indicated direction(s) lead to a motorway. </span> [[Image:United Kingdom images gbr image2550 jpg.png|368px|United Kingdom images gbr image2550 jpg.png|link=Contact]]';
似乎爲我工作,我在這裏試過你的代碼HTTPS ://www.functions-online.com/preg_replace.html – PeterPan666
嘗試更換['\ [\ [圖片:[^]] + =鏈接[^]] +]]'與'[noinclude] $ 0/noinclude ]'](https://regex101.com/r/vB0tL5/1) –