Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from RickvdStaaij/feature/multibyte-support
Feature: Add Multibyte support
- Loading branch information
Showing
29 changed files
with
313 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Multibyte encoding | ||
|
||
To make sure your values get filtered correctly, we have to use the multi-byte (`mb_`) functionality from PHP. With | ||
this feature comes the ability to set the encoding format on the filter. | ||
|
||
```php | ||
$filter = new Particle\Filter\Filter; | ||
|
||
$filter->setEncodingFormat('utf-8'); // or another format if you like | ||
``` | ||
|
||
If you don't use the function above, the string adaptive functions from php will use the default value from your | ||
php.ini, which is most likely UTF-8. | ||
|
||
**Note:** Make sure that you set the encoding format on before you add any filter rules to the filter, otherwise the | ||
default encoding format will be used on the filter rules defined before that setEncodingFormat. | ||
|
||
### Converting values to a specific encoding format | ||
|
||
If you know data is coming in in a different encoding format than what you want to be working with, you can use the | ||
encode filter rule to convert the value. | ||
|
||
```php | ||
$filter->value('first_name')->encode('UTF-8')->upper(); | ||
``` | ||
|
||
### Using multibyte a regex | ||
|
||
If you want to use multi-byte encoded regex, the following php functions should be used: | ||
|
||
```php | ||
mb_regex_encoding('UTF-8'); | ||
``` | ||
|
||
These functions are not included in Particle\Filter as it might lead to unexpected behaviour. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Particle. | ||
* | ||
* @link http://github.com/particle-php for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com) | ||
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License | ||
*/ | ||
namespace Particle\Filter\FilterRule; | ||
|
||
use Particle\Filter\FilterRule; | ||
|
||
/** | ||
* Class Encode | ||
* | ||
* @package Particle\Filter\FilterRule | ||
*/ | ||
class Encode extends FilterRule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $toEncoding; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $fromEncoding; | ||
|
||
/** | ||
* @param string|null $toEncoding | ||
* @param string|null $fromEncoding | ||
*/ | ||
public function __construct($toEncoding = null, $fromEncoding = null) | ||
{ | ||
if ($toEncoding === null) { | ||
$toEncoding = $this->encodingFormat; | ||
} | ||
|
||
$this->toEncoding = $toEncoding; | ||
$this->fromEncoding = $fromEncoding; | ||
} | ||
|
||
/** | ||
* Changes encoding of the value | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
if ($this->toEncoding === null) { | ||
return $value; | ||
} | ||
|
||
if ($this->fromEncoding === null) { | ||
return mb_convert_encoding($value, $this->toEncoding); | ||
} | ||
|
||
return mb_convert_encoding($value, $this->toEncoding, $this->fromEncoding); | ||
} | ||
} |
Oops, something went wrong.