1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-09 14:56:31 +02:00

html encode and decode

This commit is contained in:
Jarrett Croll
2015-06-16 12:27:20 -04:00
parent f1009d0267
commit f9729788c4
6 changed files with 146 additions and 0 deletions

View File

@@ -28,6 +28,8 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js.
* [getEncoding](#getencoding) * [getEncoding](#getencoding)
* [hasLowerCase](#haslowercase) * [hasLowerCase](#haslowercase)
* [hasUpperCase](#hasuppercase) * [hasUpperCase](#hasuppercase)
* [htmlDecode](#htmldecode)
* [htmlEncode](#htmlencode)
* [humanize](#humanize) * [humanize](#humanize)
* [insert](#insert) * [insert](#insert)
* [isAlpha](#isalpha) * [isAlpha](#isalpha)
@@ -433,6 +435,32 @@ S::create('fòô bàř', 'UTF-8')->hasUpperCase();
S::hasUpperCase('fòô bàř', 'UTF-8'); // false 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 #### humanize
$stringy->humanize() $stringy->humanize()

View File

@@ -866,4 +866,30 @@ class StaticStringy
return (string) Stringy::create($str, $encoding) return (string) Stringy::create($str, $encoding)
->regexReplace($pattern, $replacement, $options, $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);
}
} }

View File

@@ -1461,4 +1461,30 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
return static::create($str, $this->encoding); 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);
}
} }

View File

@@ -1023,4 +1023,26 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8') array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
); );
} }
public function htmlEncodeProvider()
{
return array(
array('&', '&'),
array('"', '"'),
array(''', "'", ENT_QUOTES),
array('&lt;', '<'),
array('&gt;', '>'),
);
}
public function htmlDecodeProvider()
{
return array(
array('&', '&amp;'),
array('"', '&quot;'),
array("'", '&#039;', ENT_QUOTES),
array('<', '&lt;'),
array('>', '&gt;'),
);
}
} }

View File

@@ -647,4 +647,24 @@ class StaticStringyTestCase extends CommonTest
$this->assertInternalType('string', $result); $this->assertInternalType('string', $result);
$this->assertEquals($expected, $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);
}
} }

View File

@@ -911,4 +911,28 @@ class StringyTestCase extends CommonTest
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy); $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);
}
} }