Skip to content

Commit e70ecfc

Browse files
[10.x] Add isMatch method to Str and Stringable helpers (#46303)
* [10.x] Add isMatch method to Str and Stringable helpers * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 48afe79 commit e70ecfc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,32 @@ public static function match($pattern, $subject)
610610
return $matches[1] ?? $matches[0];
611611
}
612612

613+
/**
614+
* Determine if a given string matches a given pattern.
615+
*
616+
* @param string|iterable<string> $pattern
617+
* @param string $value
618+
* @return bool
619+
*/
620+
public static function isMatch($pattern, $value)
621+
{
622+
$value = (string) $value;
623+
624+
if (! is_iterable($pattern)) {
625+
$pattern = [$pattern];
626+
}
627+
628+
foreach ($pattern as $pattern) {
629+
$pattern = (string) $pattern;
630+
631+
if (preg_match($pattern, $value) === 1) {
632+
return true;
633+
}
634+
}
635+
636+
return false;
637+
}
638+
613639
/**
614640
* Get the string matching the given pattern.
615641
*

src/Illuminate/Support/Stringable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ public function match($pattern)
440440
return new static(Str::match($pattern, $this->value));
441441
}
442442

443+
/**
444+
* Determine if a given string matches a given pattern.
445+
*
446+
* @param string|iterable<string> $pattern
447+
* @return bool
448+
*/
449+
public function isMatch($pattern)
450+
{
451+
return Str::isMatch($pattern, $this->value);
452+
}
453+
443454
/**
444455
* Get the string matching the given pattern.
445456
*

0 commit comments

Comments
 (0)