Skip to content

Commit d7321a6

Browse files
committed
update some info
1 parent dcb7c6d commit d7321a6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/ColorTag.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class ColorTag
1717
// regex used for removing color tags
1818
private const STRIP_TAG = '/<[\/]?[a-zA-Z=;]+>/';
1919

20+
// Regex to match tags/
21+
private const COLOR_TAG = '/<([a-zA-Z=;]+)>(.*?)<\/\\1>/s';
22+
2023
/**
2124
* alias of the wrap()
2225
* @param string $text
@@ -42,6 +45,19 @@ public static function wrap(string $text, string $tag): string
4245
return "<$tag>$text</$tag>";
4346
}
4447

48+
/**
49+
* @param string $text
50+
* @return array
51+
*/
52+
public static function matchAll(string $text): array
53+
{
54+
if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
55+
return [];
56+
}
57+
58+
return $matches;
59+
}
60+
4561
public static function parse(string $text): string
4662
{
4763

test/ColorTagTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
*/
1818
class ColorTagTest extends TestCase
1919
{
20+
public function testMatchAll(): void
21+
{
22+
$ret = ColorTag::matchAll('<tag>text0</tag> or <info>text1</info>');
23+
24+
$this->assertCount(3, $ret);
25+
// tag
26+
$this->assertSame('tag', $ret[1][0]);
27+
$this->assertSame('info', $ret[1][1]);
28+
// content
29+
$this->assertSame('text0', $ret[2][0]);
30+
}
31+
2032
public function testStrip(): void
2133
{
2234
$text = ColorTag::strip('<tag>text</tag>');

0 commit comments

Comments
 (0)