Add modifiers match, ematch, replace, ereplace, split, esplit, join with documentation [2]

This commit is contained in:
bzick 2014-07-10 23:28:27 +04:00
parent 702dbfd88d
commit 8e48d1aaee
7 changed files with 132 additions and 0 deletions

18
docs/mods/ematch.md Normal file
View File

@ -0,0 +1,18 @@
Modifier ematch
==============
Perform a regular expression match.
[Read more](http://www.php.net/manual/en/reference.pcre.pattern.syntax.php) about regular expression.
```
{$string|ematch:$pattern}
```
Searches `$string` for a match to the regular expression given in `$pattern`.
```smarty
{if $color|ematch:'/^gr[ae]y$/i'}
some form of gray ...
{/if}
```

23
docs/mods/ereplace.md Normal file
View File

@ -0,0 +1,23 @@
Modifier ereplace
=================
Perform a regular expression search and replace.
[Read more](http://www.php.net/manual/en/reference.pcre.pattern.syntax.php) about regular expression.
```
{$string|replace:$pattern:$replacement}
```
Searches `$string` for matches to `$pattern` and replaces them with `$replacement`.
`$replacement` may contain references of the form `\n`, `$n` or `${n}`, with the latter form being the preferred one.
Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99,
and `\0` or `$0` refers to the text matched by the whole pattern.
Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern.
To use backslash in replacement, it must be doubled.
```smarty
{var $string = 'April 15, 2014'}
{$string|ereplace:'/(\w+) (\d+), (\d+)/i':'${1}1, $3'} {* April1, 2014 *}
```

19
docs/mods/esplit.md Normal file
View File

@ -0,0 +1,19 @@
Modifier esplit
===============
Split string by a regular expression.
[Read more](http://www.php.net/manual/en/reference.pcre.pattern.syntax.php) about regular expression.
```
{$string|esplit:$pattern = '/,\s*/'}
```
My default modifier split string by comma with spaces.
```smarty
{var $fruits1 = "banana, apple, pear"|esplit}
$fruits1 is array ["banana", "apple", "pear"]
{var $fruits2 = "banana; apple; pear"|esplit:'/;\s/'} is ["banana", "apple", "pear"]
$fruits2 is array ["banana", "apple", "pear"] too
```

16
docs/mods/join.md Normal file
View File

@ -0,0 +1,16 @@
Modifier split
==============
Join array elements with a string.
```
{$string|join:$delimiter = ","}
```
Returns an array of strings, each of which is a substring of `$string` formed by splitting it on boundaries formed by the string `$delimiter`.
```smarty
{var $fruits1 = ["banana", "apple", "pear"]}
{$fruits1|join} output banana, apple, pear
{$fruits1|join:" is not "} output banana is not apple is not pear
```

24
docs/mods/match.md Normal file
View File

@ -0,0 +1,24 @@
Modifier match
==============
Match string against a pattern.
The average user may be used to shell patterns or at least in their simplest form to `?` and `*` wildcards so using `match`
instead of `ematch` for frontend search expression input may be way more convenient for non-programming users.
```
{$string|match:$pattern}
```
Special pattern symbols:
* `?` — match one or zero unknown characters. `?at` matches `Cat`, `cat`, `Bat` or `bat`, `but` not `at`.
* `*` — match any number of unknown characters. `Law*` matches `Law`, `Laws`, or `Lawyer`.
* `[characters]` — Match a character as part of a group of characters. `[CB]at` matches `Cat` or `Bat` but not `cat`, `rat` or `bat`.
* `\` - Escape character. `Law\*` will only match `Law*`
```smarty
{if $color|match:"*gr[ae]y"}
some form of gray ...
{/if}
```

14
docs/mods/replace.md Normal file
View File

@ -0,0 +1,14 @@
Modifier replace
================
Replace all occurrences of the search string with the replacement string
```
{$string|replace:$search:$replace}
```
This modifier returns a string with all occurrences of `$search` in subject replaced with the given `$replace` value.
```smarty
{$fruits|replace:"pear":"orange"}
```

18
docs/mods/split.md Normal file
View File

@ -0,0 +1,18 @@
Modifier split
==============
Split a string by string
```
{$string|split:$delimiter = ","}
```
Returns an array of strings, each of which is a substring of `$string` formed by splitting it on boundaries formed by the string `$delimiter`.
```smarty
{var $fruits1 = "banana,apple,pear"|split}
$fruits1 is array ["banana", "apple", "pear"]
{var $fruits2 = "banana,apple,pear"|split:',apple,'}
$fruits2 is array ["banana", "pear"]
```