replace(), you need to explicitly use one of
only(int) methods, to express how many
replacements should be done.
Callback passed to
replace()->callback() will only be invoked:
all()- as many times as there are occurrences matched in the subject.
only(int)- the same as
all(), but up to an
first()- once if an occurrence is matched; or not at all if it's not.
Replacing all matched occurrences is the most common use-case:
You can also limit the amount of replacements done with
We believe that returning anything, that's not a string,
Detail or a group can be a sign of a bug!
Moreover, converting them silently would break our "Explicity rule".
If you'd like to replace an occurrence with a numeric value (for example
'12'), an empty string or
literals - just return them as
You can call
replace()->callback() for any valid PHP
callable which accepts one string parameter (or no parameters)
In this example,
Detail will be cast to string, which is the same as calling