Added methods Str::stripTags() and Stringable::stripTags(). (#4129)

This commit is contained in:
Deeka Wong 2021-10-13 11:37:40 +08:00 committed by GitHub
parent c1f72a04ec
commit 6d559e1033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 0 deletions

View File

@ -1,5 +1,9 @@
# v2.2.12 - TBD
## Added
- [#4129](https://github.com/hyperf/hyperf/pull/4129) Added methods `Str::stripTags()` and `Stringable::stripTags()`.
# v2.2.11 - 2021-10-11
## Fixed

View File

@ -566,6 +566,16 @@ class Str
return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
}
/**
* Strip HTML and PHP tags from the given string.
*
* @param null|string|string[] $allowedTags
*/
public static function stripTags(string $value, $allowedTags = null): string
{
return strip_tags($value, $allowedTags);
}
/**
* Convert the given string to upper-case.
*/

View File

@ -569,6 +569,17 @@ class Stringable implements JsonSerializable
return new static(Str::start($this->value, $prefix));
}
/**
* Strip HTML and PHP tags from the given string.
*
* @param null|string|string[] $allowedTags
* @return static
*/
public function stripTags($allowedTags = null)
{
return new static(strip_tags($this->value, $allowedTags));
}
/**
* Convert the given string to upper-case.
*

View File

@ -142,4 +142,20 @@ class StrTest extends TestCase
$this->assertTrue(Str::startsWith('http://www.hyperf.io', 'http://'));
$this->assertTrue(Str::startsWith('https://www.hyperf.io', ['http://', 'https://']));
}
public function testStripTags()
{
$this->assertSame('beforeafter', Str::stripTags('before<br>after'));
$this->assertSame('before<br>after', Str::stripTags('before<br>after', '<br>'));
$this->assertSame('before<br>after', Str::stripTags('<strong>before</strong><br>after', '<br>'));
$this->assertSame('<strong>before</strong><br>after', Str::stripTags('<strong>before</strong><br>after', '<br><strong>'));
if (PHP_VERSION_ID >= 70400) {
$this->assertSame('<strong>before</strong><br>after', Str::stripTags('<strong>before</strong><br>after', ['<br>', '<strong>']));
}
if (PHP_VERSION_ID >= 80000) {
$this->assertSame('beforeafter', Str::stripTags('before<br>after', null));
}
}
}

View File

@ -95,4 +95,20 @@ class StringableTest extends TestCase
$this->assertTrue(Str::of('http://www.hyperf.io')->startsWith('http://'));
$this->assertTrue(Str::of('https://www.hyperf.io')->startsWith(['http://', 'https://']));
}
public function testStripTags()
{
$this->assertSame('beforeafter', (string) Str::of('before<br>after')->stripTags());
$this->assertSame('before<br>after', (string) Str::of('before<br>after')->stripTags('<br>'));
$this->assertSame('before<br>after', (string) Str::of('<strong>before</strong><br>after')->stripTags('<br>'));
$this->assertSame('<strong>before</strong><br>after', (string) Str::of('<strong>before</strong><br>after')->stripTags('<br><strong>'));
if (PHP_VERSION_ID >= 70400) {
$this->assertSame('<strong>before</strong><br>after', (string) Str::of('<strong>before</strong><br>after')->stripTags(['<br>', '<strong>']));
}
if (PHP_VERSION_ID >= 80000) {
$this->assertSame('beforeafter', (string) Str::of('before<br>after')->stripTags(null));
}
}
}