diff --git a/README.md b/README.md index 3dcb3bd..8acc65a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js. * [getEncoding](#getencoding) * [hasLowerCase](#haslowercase) * [hasUpperCase](#hasuppercase) + * [htmlDecode](#htmldecode) + * [htmlEncode](#htmlencode) * [humanize](#humanize) * [insert](#insert) * [isAlpha](#isalpha) @@ -433,6 +435,32 @@ S::create('fòô bàř', 'UTF-8')->hasUpperCase(); S::hasUpperCase('fòô bàř', 'UTF-8'); // false ``` +#### htmlDecode + +$stringy->htmlDecode() + +S::htmlDecode(string $str [, int $flags, string $encoding ]) + +Convert all HTML entities to their applicable characters. + +```php +S::create('&')->htmlDecode(); +S::htmlDecode('&'); // '&' +``` + +#### htmlEncode + +$stringy->htmlEncode() + +S::htmlEncode(string $str [, int $flags, string $encoding ]) + +Convert all applicable characters to HTML entities. + +```php +S::create('&')->htmlEncode(); +S::htmlEncode('&'); // '&' +``` + #### humanize $stringy->humanize() diff --git a/src/StaticStringy.php b/src/StaticStringy.php index 78734db..0462395 100644 --- a/src/StaticStringy.php +++ b/src/StaticStringy.php @@ -866,4 +866,30 @@ class StaticStringy return (string) Stringy::create($str, $encoding) ->regexReplace($pattern, $replacement, $options, $encoding); } + + /** + * Convert all applicable characters to HTML entities. + * + * @param string $str The string to encode. + * @param int|null $flags See http://php.net/manual/en/function.htmlentities.php + * @param string $encoding The character encoding + * @return Stringy Object with the resulting $str after being html encoded. + */ + public static function htmlEncode($str, $flags = ENT_COMPAT, $encoding = null) + { + return (string) Stringy::create($str, $encoding)->htmlEncode($flags); + } + + /** + * Convert all HTML entities to their applicable characters. + * + * @param string $str The string to decode. + * @param int|null $flags See http://php.net/manual/en/function.html-entity-decode.php + * @param string $encoding The character encoding + * @return Stringy Object with the resulting $str after being html decoded. + */ + public static function htmlDecode($str, $flags = ENT_COMPAT, $encoding = null) + { + return (string) Stringy::create($str, $encoding)->htmlDecode($flags); + } } diff --git a/src/Stringy.php b/src/Stringy.php index 4729c83..7c9aa79 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -1461,4 +1461,30 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess return static::create($str, $this->encoding); } + + /** + * Convert all applicable characters to HTML entities. + * + * @param int|null $flags See http://php.net/manual/en/function.htmlentities.php + * @return Stringy Object with the resulting $str after being html encoded. + */ + public function htmlEncode($flags = ENT_COMPAT) + { + $str = htmlentities($this->str, $flags, $this->encoding); + + return static::create($str, $this->encoding); + } + + /** + * Convert all HTML entities to their applicable characters. + * + * @param int|null $flags See http://php.net/manual/en/function.html-entity-decode.php + * @return Stringy Object with the resulting $str after being html decoded. + */ + public function htmlDecode($flags = ENT_COMPAT) + { + $str = html_entity_decode($this->str, $flags, $this->encoding); + + return static::create($str, $this->encoding); + } } diff --git a/tests/CommonTest.php b/tests/CommonTest.php index 9795702..061773c 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -1023,4 +1023,26 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8') ); } + + public function htmlEncodeProvider() + { + return array( + array('&', '&'), + array('"', '"'), + array(''', "'", ENT_QUOTES), + array('<', '<'), + array('>', '>'), + ); + } + + public function htmlDecodeProvider() + { + return array( + array('&', '&'), + array('"', '"'), + array("'", ''', ENT_QUOTES), + array('<', '<'), + array('>', '>'), + ); + } } diff --git a/tests/StaticStringyTest.php b/tests/StaticStringyTest.php index d8a4739..3a4266f 100644 --- a/tests/StaticStringyTest.php +++ b/tests/StaticStringyTest.php @@ -647,4 +647,24 @@ class StaticStringyTestCase extends CommonTest $this->assertInternalType('string', $result); $this->assertEquals($expected, $result); } + + /** + * @dataProvider htmlEncodeProvider() + */ + public function testHtmlEncode($expected, $str, $flags = ENT_COMPAT, $encoding = null) + { + $result = S::htmlEncode($str, $flags, $encoding); + $this->assertInternalType('string', $result); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider htmlDecodeProvider() + */ + public function testHtmlDecode($expected, $str, $flags = ENT_COMPAT, $encoding = null) + { + $result = S::htmlDecode($str, $flags, $encoding); + $this->assertInternalType('string', $result); + $this->assertEquals($expected, $result); + } } diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 617c065..3471877 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -911,4 +911,28 @@ class StringyTestCase extends CommonTest $this->assertEquals($expected, $result); $this->assertEquals($str, $stringy); } + + /** + * @dataProvider htmlEncodeProvider() + */ + public function testHtmlEncode($expected, $str, $flags = ENT_COMPAT, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->htmlEncode($flags); + $this->assertStringy($result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + + /** + * @dataProvider htmlDecodeProvider() + */ + public function testHtmlDecode($expected, $str, $flags = ENT_COMPAT, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->htmlDecode($flags); + $this->assertStringy($result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } }