Skip to content

Commit 4efdd76

Browse files
committed
Include match examples in the code block
1 parent 683a671 commit 4efdd76

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

components/string.rst

+10-16
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ Methods to Search and Replace
318318
// checks if the string contents are exactly the same as the given contents
319319
u('foo')->equalsTo('foo'); // true
320320

321+
// checks if the string content match the given regular expression.
322+
// You can pass flags for preg_match() as second argument. If PREG_PATTERN_ORDER
323+
// or PREG_SET_ORDER are passed, preg_match_all() will be used.
324+
u('avatar-73647.png')->match('/avatar-(\d+)\.png/');
325+
// result = ['avatar-73647.png', '73647']
326+
u('avatar-73647.png')->match('/avatar-(\d+)(-\d+)?\.png/', \PREG_UNMATCHED_AS_NULL);
327+
// result = ['avatar-73647.png', '73647', null]
328+
u('206-555-0100 and 800-555-1212')->match('/\d{3}-\d{3}-\d{4}/', \PREG_PATTERN_ORDER);
329+
// result = [['206-555-0100', '800-555-1212']]
330+
321331
// checks if the string contains any of the other given strings
322332
u('aeiou')->containsAny('a'); // true
323333
u('aeiou')->containsAny(['ab', 'efg']); // false
@@ -354,22 +364,6 @@ Methods to Search and Replace
354364

355365
The ``containsAny()`` method was introduced in Symfony 5.1.
356366

357-
::
358-
359-
You can use ``match()`` to search with a Regular Expression::
360-
361-
u('avatar-73647.png')->match('/avatar-(\d+)\.png/');
362-
// result = ['avatar-73647.png', '73647']
363-
364-
By default, PHP's ``preg_match()`` is used, and you can pass search flags as second argument::
365-
366-
$string->match('/(a)(b)/', \PREG_UNMATCHED_AS_NULL);
367-
368-
When passing ``\PREG_PATTERN_ORDER`` or ``\PREG_SET_ORDER``, PHP's ``preg_match_all()`` is used.
369-
Multiple flags can be set with the `|` operator::
370-
371-
$string->match('/(a)(b)/', \PREG_PATTERN_ORDER|\PREG_UNMATCHED_AS_NULL);
372-
373367
Methods to Join, Split, Truncate and Reverse
374368
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
375369

0 commit comments

Comments
 (0)