PHP preg_replace – optional one character regex example – sandbox

PHP preg_replace example for optional one char match. Here the regular expression (regex) pattern contains question mark (“?”) and the char precedded by “?” is matched optionally in the content. Also note that the engine uses greedy match. That means it matches the optional char whenever possible.

$replaced_content = preg_replace($pattern, $replacement, $content [, int $limit = -1 [, int &$count]])
Input
Pattern example: /abc/i
Replacement
Content