Glad to hear it helped you out.
Very little change is required to make it work with the second set of tags. A little bit of trivia to make you understand how it works, and in the future you should be able to do this yourself.
The first pattern, "/[TAG(\d)](.)[\/TAG\1]/Uus", uses two subpatterns, which are the parts enclosed in parenthesis (). You can refer to subpatterns from within the pattern itself, using back references \1, \2 etc. Matches against these subpatterns are also put in arrays which are found in the third function argument of preg_match_all.
That is, $match[1] contains all matches of (\d), and $match[2] contains all matches of (.), which you could have referred to in the pattern with \2.
Do note that in a double quote enclosed strings in PHP, you need to escape the backslash \ to get the two characters \ and 1:
"\1" becomes \1, which is not the same as the character "\1".
So, (\d), is a subpattern matching one digit, which is back referenced in the closing tag above. All that's needed to match the new tags is to create a subpattern for several characters and put that at the beginning of the tag.
$str = "[NOUVEAU_CHAPITRE]Ma première femme[/NOUVEAU_CHAPITRE][SOUS_CHAPITRE]Rencontre[/SOUS_CHAPITRE]Il m'est à peu près impossible aujourd'hui de me souvenir...[NOUVEAU_CHAPITRE]L'infortuné comique[/NOUVEAU_CHAPITRE]";
$re = "/\[(.*)_CHAPITRE](.*)\[\/\\1_CHAPITRE]/Uus";
preg_match_all($re, $str, $match);
print_r($match);
if (count($match[0]) > 0) {
for ($i = 0; $i < count($match[1]); ++$i) {
if ($match[1][$i] == "NOUVEAU") {
$word[] = $match[2][$i];
}
else if ($match[1][$i] == "SOUS") {
$link[] = $match[2][$i];
}
}
}
print_r($word);
print_r($link);
exit;
Output:
Array {
0 => {
0 => [NOUVEAU_CHAPITRE]Ma première femme[/NOUVEAU_CHAPITRE]
1 => [SOUS_CHAPITRE]Rencontre[/SOUS_CHAPITRE]
2 => [NOUVEAU_CHAPITRE]L'infortuné comique[/NOUVEAU_CHAPITRE]
}
1 => {
0 => NOUVEAU
1 => SOUS
2 => NOUVEAU
}
2 => {
0 => Ma première femme
1 => Rencontre
2 => L'infortuné comique
}
}
Array {
0 => Ma première femme
1 => L'infortuné comique
}
Array {
0 => Rencontre
}