mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-09 06:46:40 +02:00
Merge pull request #91 from jcroll/htmlEncode
Added htmlEncode and htmlDecode
This commit is contained in:
28
README.md
28
README.md
@@ -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()
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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('<', '<'),
|
||||||
|
array('>', '>'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function htmlDecodeProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('&', '&'),
|
||||||
|
array('"', '"'),
|
||||||
|
array("'", ''', ENT_QUOTES),
|
||||||
|
array('<', '<'),
|
||||||
|
array('>', '>'),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user