What are some common mistakes to avoid when limiting the number of replacements in preg_replace() in PHP?

When limiting the number of replacements in preg_replace() in PHP, a common mistake to avoid is not specifying the limit parameter correctly. If the limit is not set or set to -1, it will replace all occurrences instead of the desired number. To avoid this, make sure to set the limit parameter to the desired number of replacements.

// Incorrect way without specifying the limit parameter
$result = preg_replace('/pattern/', 'replacement', $input);

// Correct way with specifying the limit parameter
$result = preg_replace('/pattern/', 'replacement', $input, 2); // Limiting to 2 replacements