<?php function convert(string $txt): string { do { $txt = preg_replace_callback_array( [ '~\[([bi])](.*?)\[/\1]~' => fn($m) => sprintf('<%1$s>%2$s</%1$s>', $m[1], $m[2]), '~\[img](.*?)\[/img]~' => 'foo', ], $txt, -1, $count ); } while ($count); return $txt; } function foo(array $m): string { return '<img src="' . $m[1] . '">'; } echo convert("text1 [img]path/src[/img] text2 [b]bold [i]nested string[/i][/b] [img]another/path/to/file[/img] [b]nice[/b] lingering end bbtag [/b] and [b]unclosed");
You have javascript disabled. You will not be able to edit any code.