preg_replace
Performs a regular expression search and replace operation on a string or array of strings.
Parameters
main.php
Return Values
string|array|null
Returns string/array with replacements made, or null on error
On success:Modified string/array with pattern matches replaced by replacement
On failure:null if an error occurred during processing
Notes
- Uses Perl-compatible regular expressions (PCRE)
- Pattern must include delimiters (e.g., '/pattern/flags')
- Supports backreferences ($1, $2) and various regex flags
- Use preg_quote() to escape special regex characters in literals
Changelog
- 4.0.0: Function was introduced
- 7.0.0: The deprecated 'e' modifier was removed
- 7.2.0: Improved error handling
Related Functions
preg_match
RegExPerforms a regular expression match against a string and returns whether a match was found.
regexpatternvalidation
str_replace
StringReplaces all occurrences of search string with replacement string.
replacesanitizeclean
strtr
StringTranslates characters or replaces substrings in a string using either character mapping or replacement pairs.
replacetranslatesubstitute