mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added methods Str::stripTags()
and Stringable::stripTags()
. (#4129)
This commit is contained in:
parent
c1f72a04ec
commit
6d559e1033
@ -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
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user