lecter007
lecter007

Reputation: 21

php preg_replace from [[ to ]]

I have a text like this:

$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]];

and I want to add [noinclude] before text that has |link= between [[ and ]] and add [/noinclude] after the ]]. So I want to add it to the first and third image. When I try it like this:

preg_replace('/\[\[Image:(.*?)\]\]/', '[noinclude][[Image:$1]][/noinclude]', $t); 

it wraps all 3 images. And when I try it like this:

preg_replace('/\[\[Image:(.*?)\|link=(.*?)\]\]/', '[noinclude][[Image:$1|link=$2]][/noinclude]', $t);

the result is:

[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] 

so it adds my tags correctly to the first one, but when it gets to the second image, it doesn't stop at ]] of the second image, but continues to the ]] of the third image (as it's looking for the |link)

How can I force it to stop at ]], if there is |link= between [[ ]] ?

Thanks!

UPDATE: And what if the text is more complicated? Like this:

$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]]';

Upvotes: 2

Views: 169

Answers (2)

mitkosoft
mitkosoft

Reputation: 5316

You can use preg_split() togethter with 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);

Final result is:

[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]

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You can simply use the following regex

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

This'll capture those links which contains |link= between [[Image and ]]. So your code looks like as

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

Upvotes: 1

Related Questions