Match details
When using pattern()->match()
and pattern()->replace->callback()
, some methods returns Detail
details object.
The details can be used to get concise information about the matched occurrence, such as its value (i.e. "the whole match"), capturing groups and their character/byte offsets, indices, other matches as well as the used subject (although it could also be pass as a closure parameter) and more.
For example, to read the offset at which the occurrence was matched, use Detail.offset()
:
#
OverviewUsing Detail
details, you gain access to:
text()
- value of a matched occurrencetoInt()
/isInt()
which allow you to handle integers safelysubject()
- subject against which the pattern was matchedindex()
- ordinal value of a matched occurrence- offsets of matched values in the subject (UTF-8 safe):
- byte versions of the methods:
byteOffset()
- position of the occurrence in bytesbyteLength()
- length of the matched occurrence in bytesbyteTail()
- position after the occurrence in bytes (tail=offset+length)
all()
- other matched occurrences- details about capturing groups, in the next chapter: Capturing groups
#
Matched textThere are 6 similar ways to get the value of the matched occurrence.
or you can just accept string
in the callback signature.
All of them are redundant and equal to each other. Their redundancy comes from the fact the there are a few ways of
casting an object to string in PHP, casting Detail
to string is the same as getting text()
in T-Regx, and that the
whole match is also group 0
in regular expressions.
There's also Unicode-safe method length()
which returns the length of a matched text.
#
IntegersMethod isInt()
returns true
if, and only if, the matched occurrence is numeric. And by "numeric", we mean "real" numeric,
not PHP numeric:
- String values considered valid integers:
'14'
,'-14'
,'000'
- Strings that aren't treated as valid integers:
'+14'
,' 10'
,'10 '
,''
,' '
,'0.0'
,'0,0'
,
The string is considered a valid integer if:
- contains only
0-9
characters, and more than 1 of them (so00
is also a valid integer, but''
isn't) - optionally starts with only one
-
sign - its numeric representation is:
- higher or equal
PHP_INT_MIN
(-9223372036854775808
) - lower or equal
PHP_INT_MAX
(9223372036854775807
)
- higher or equal
- doesn't contain any other characters
#
Checking and parsingThere are two methods regarding integers: isInt()
and toInt()
.
$detail->isInt()
returns true
/false
depending on whether the matched occurrence is numeric. toInt()
returns said numeric occurrence as an integer, or throws IntegerFormatException
instead.
note
It's implemented with filter_var()
, but you can think of it as /^-?[0-9]+$/
with max/min values check.
#
SubjectTo get the subject in your callback, use Detail.subject()
:
This is equivalent to storing the subject in a variable and using it in your closure.
#
Ordinal value (index)Detail.index()
returns the ordinal number of a matched occurrence.
In this example, we'll modify every second word:
#
OffsetsDetail.offset()
can be used to get the offset of the matched occurrence in the subject. Detail.offset()
is unicode
character safe and returns offset in characters, whereas Detail.byteOffset()
returns the offset in bytes.
Here's what the numbers mean:
In other words, offset()
treats bytes [226, 130, 172]
as one unicode character (euro sign €
) and counts them as
one; whereas byteOffset()
would count them as three.
Use:
offset()
with functions:mb_substr()
,mb_strpos()
byteOffset()
with functions:substr()
,strpos()
#
TailMethod Detail.tail()
simply returns the position of the last character in a matched occurrence.
There's also Detail.byteTail()
which returns the tail in bytes, instead of characters.
#
Other occurrencesDetail
has access to other matched occurrences:
Detail.all()
- for whole matches (likeDetail.text()
)Detail.group().all()
- for capturing groups (likeDetail.group().text()
)
Even if you use first()
or only(int)
methods, Detail.all()
always returns unlimited occurrences.
#
GroupsWith Detail.group(string|int)
, you can easily retrieve capturing groups.
Just like with Detail
, retrieving matched occurrence value is done with text()
method or by casting it to string
.
More about capturing groups can be found in the next section: Capturing groups.