However, when using
pattern()->replace() the callback receives
ReplaceMatch details object.
Detail object, so they have exactly alike interfaces.
ReplaceMatch has two separate methods:
modifiedSubject()- current state of a subject being built.
modifiedOffset()- occurrence's offset, but according to a current the
Given a pattern, that matches capitalized words:
having iterated the subject looking for
[A-Z][a-z]+ - for each
Detail the result of
method would always be the same. There are 4 occurrences matched by the pattern, so callback is invoked 4 times,
and each time
$detail->subject() is equal to:
However, results of
ReplaceMatch.modifiedSubject() would also contain results of previous replacements.
$result would be equal to
Have you iterated the subject looking for
[A-Z][a-z]+, these would be the results of
But, if instead of
ReplaceMatch.offset() you use
ReplaceMatch.modifiedOffset(), these are the results:
modifiedOffset() as well as
modifiedSubject() are available for groups (which when replacing are of type
ReplaceDetailGroup extends DetailGroup.
When used on group, the
modifiedOffset() returns the offset at which the captured group is present in the
modified subject, not the offset at which the whole match was captured.
modifiedSubject() for groups returns exactly the same value as
But be sure, each and every of those examples only uses one call to
simply remembers the length of the replacement returned from
callback(), and adds it to
When used on
ReplaceDetail (whole match) or
ReplaceDetailGroup (capturing group), method
returns character position.
To read byte position, use