Specifying limits is done to relieve you from brain strain - so you can immediately recognize author's intentions.
All occurrences -
Limited occurrences -
Read on, to learn more about replacing with a callback.
Normally, had you passed a replacement to
preg_replace(), that contains a backslash or a dollar sign followed by a
$2) - that reference would be replaced by a corresponding capturing group (or by an empty string,
if the group wasn't matched).
Resolving such references won't happen using
You can be sure, what's put into
with() will certainly be present unchanged in your final result.
Some replacement strings containing a backslash or a dollar sign (like file system paths, URL addresses or even user input) might interfere with logic and cause bugs that are very hard to find.
Neither of types of references are resolved:
If you, however, would like to intentionally use regular expression references and have validated your input
against an unexpected
$ - feel free to use
withReferences() which will resolve replacement references.
The only valid use-cases for
- Performance (since
preg_replace(), and not
- Migration from
preg_replace()used in legacy codebase
withReferences() is not recommended. Instead, try using
We don't encourage its usage, but we're keeping it nonetheless, to allow users to decide for themselves whether they want to use T-Regx API, or stay with PHP replace style.
There are times when you'd like to simply remove the occurrence of a pattern from your subject. To do that,
pattern()->remove() instead of
It's actually identical to calling
replace()->with('') under the hood, but it's a bit shorter and more expressive.