I'm updating my coding and moving away from preg_replace towards the function preg_replace_callback. On the first I used two arrays, which replaced all the matches in a piece of text.
I have setup the following new piece of code, but I am running into some issues:
<?php
$inhoud = " dit is een test versie, waarin [alum] staat [alum] & [fotoalbums] om [intern=test]te[/intern] vervangen<p>";
function parse_callback($match) {
$hit = $match[0];
switch ($hit){
case '[alum]':
return "<a href=\"/linktype1/\">link1</a>";
break;
case '[fotoalbums]':
return "<a href=\"/linktype2/\">link2</a>";
break;
case '[intern]':
return "<a href=\"". $match[1] ."\">$match[2]</a>";
break;
default:
//return "UNKNOWN:$match";
return var_dump($match);
}
}
$Patroon = "'\[intern=(.*?)\](.*?)\\[\/intern\]'";
$Patroon = "'\[fotoalbums\]'";
$Patroon = "'\[alum\]'";
$inhoud = preg_replace_callback($Patroon, parse_callback, $inhoud);
?>
The later two in the $Patroon are no issue, these will be updated, but I'm unable to update the first, as the match being found will be the complete string. The $match will also contain the pieces of text from $inhoud on which is has matched (.*?), but I'm unable to process these in the switch.
Any suggestions on how to resolve this issue or a better approach to the coding. The above example is just a few options which will be matched, the actual list is much larger.