2016-03-08 122 views
2

我有這樣的文字: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]]'; 
+0

似乎爲我工作,我在這裏試過你的代碼HTTPS ://www.functions-online.com/preg_replace.html – PeterPan666

+0

嘗試更換['\ [\ [圖片:[^]] + =鏈接[^]] +]]'與'[noinclude] $ 0/noinclude ]'](https://regex101.com/r/vB0tL5/1) –

回答

1

您可以簡單地使用正則表達式如下

~(\[\[Image.*\|link\=.*\]\])~ 

這會捕捉其中包含和]]之間|link=這些鏈接。所以,你的代碼看起來像作爲

echo preg_replace("~(\[\[Image.*\|link\=.*\]\])~","[noinclude]$1[/noinclude]",$str); 
+0

釷anks Uchiha!但是當文本更復雜時該怎麼辦?我剛剛更新了我原來的問題。當我運行你的代碼,它不添加結束[/ noinclude]正確的 - 它增加了[/ noinclude]到文本的盡頭...... – lecter007

0

您可以使用preg_split() togethter與strpos()

$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]]'; 
$n = array_filter(preg_split('/(?=\[\[)/', $t, -1, PREG_SPLIT_DELIM_CAPTURE)); 
foreach($n AS $key => $value){ 
    if(strpos($value, '|link') !== false){ 
     $n[$key] = '[noinclude]'.$value.'[/noinclude]'; 
    } 
} 
$t = implode($n); 

最後的結果是:

[noinclude][[Image:United Kingdom images gbr image031 jpg.png|141px|link=HomePage|United Kingdom images gbr image031 jpg.png]][/noinclude] 
[[Image:United Kingdom images gbr image030 jpg.png|141px|United Kingdom images gbr image030 jpg.png]] 
[noinclude][[Image:United Kingdom images gbr image031 jpg.png|141px|link=Contact|United Kingdom images gbr image031 jpg.png]][/noinclude] 
+0

感謝Mitkosoft!但是當文本更復雜時該怎麼辦?我剛剛更新了我原來的問題。當我運行你的代碼,它不會像第一次添加後結束[/ noinclude] - 它增加了它在文中某處進一步... – lecter007

+0

我的回答是基於你的準確輸入。在你更新的情況下,你應該使用正則表達式 - 檢查@Uchiha的解決方案,對我來說+1。 – mitkosoft