PHP preg_replace – optional character group regex example – sandbox

PHP preg_replace example for optional char group match. Here the regular expression (regex) pattern contains optional character group enclosed within parentheses and followed by questiom mark (“?”). The regex engine tries to match the either the whole group or nothing. Also note that the engine uses greedy match.

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