PHP preg_replace – back reference within pattern example – sandbox

PHP preg_replace example for back reference within pattern. Here the match is grouped using parentheses and that group is accessed using backslash followed by the number of group. For first group the number is 1. Also note that we changed the regex delimiter from slash (/) to hash (#) to make pattern more readable as slash is part of regex.

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


Click here to write/view comments