<?php $pattern = "/\[([^][|]+)(?:\|([^][]+))?\]/"; $s = "This is an example definition. Here is a link to [foo]. This is an [aliased link|bar]."; $s = preg_replace_callback($pattern, function($match){ $template = '<a href="%s">%s</a>'; return sprintf($template, array_key_exists(2, $match) ? $match[2] : $match[1], $match[1]); }, $s); echo $s;
You have javascript disabled. You will not be able to edit any code.