dd/evolutioncms-snippets-ddstringtools
Composer 安装命令:
composer require dd/evolutioncms-snippets-ddstringtools
包简介
Tools for modifying strings.
关键字:
README 文档
README
Tools for modifying strings.
Requires
- PHP >= 5.6
- (MODX)EvolutionCMS >= 1.1
- (MODX)EvolutionCMS.libraries.ddTools >= 0.60
- (MODX)EvolutionCMS.snippets.ddtypograph >= 2.6 (if the
tools->typographerparameter is used) - PHP.libraries.Parsedown >= 1.8.0-beta-7 (contains in archive)
Installation
Using (MODX)EvolutionCMS.libraries.ddInstaller
Just run the following PHP code in your sources or Console:
// Include (MODX)EvolutionCMS.libraries.ddInstaller require_once( $modx->getConfig('base_path') . 'assets/libs/ddInstaller/require.php' ); // Install (MODX)EvolutionCMS.snippets.ddStringTools \DDInstaller::install([ 'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddStringTools', ]);
- If
ddStringToolsis not exist on your site,ddInstallerwill just install it. - If
ddStringToolsis already exist on your site,ddInstallerwill check it version and update it if needed.
Manually
1. Elements → Snippets: Create a new snippet with the following data
- Snippet name:
ddStringTools. - Description:
<b>2.3</b> Tools for modifying strings.. - Category:
Core. - Parse DocBlock:
no. - Snippet code (php): Insert content of the
ddStringTools_snippet.phpfile from the archive.
2. Elements → Manage Files:
- Create a new folder
assets/snippets/ddStringTools/. - Extract the archive to the folder (except
ddStringTools_snippet.php).
Parameters description
-
inputString- Description: The input string.
- Valid values:
string- The input string can also be set as a PHP object or array (e. g. for calls through
$modx->runSnippet). In this case, it will be converted to JSON first.objectarray
- Default value:
''
-
tools- Description: List of string tools to be applied to
inputString. Tools are called in accordance with the specified order. - Valid values:
stirngJsonObject— as JSONstringHjsonObject— as HJSONstringQueryFormatted— as Query string- It can also be set as a PHP object or array (e. g. for calls through
$modx->runSnippet).arrayAssociativeobject
- Default value:
'{}'
- Description: List of string tools to be applied to
-
tools->{$toolName}- Description: A tool, when the key is the tool name and the value is the tool parameters.
- Tool names are case insensitive (the following names are equal:
caseConverter,Caseconverter,caseconverter, etc).
- Tool names are case insensitive (the following names are equal:
- Valid values:
object— an object with tool parameters (see below)boolean— for simple tools without parameters or if you need to use default parameters (if possible), just passtrue
- Default value: —.
- Description: A tool, when the key is the tool name and the value is the tool parameters.
Case converter
-
tools->caseConverter- Description: Perform case folding on a string. Unicode is supported.
- Valid values:
object - Default value: —.
-
tools->caseConverter->toLower- Description: Make a string lowercase.
- Valid values:
boolean - Default value:
false
-
tools->caseConverter->toUpper- Description: Make a string uppercase.
- Valid values:
boolean - Default value:
false
Markdown parser
-
tools->markdownParser- Description: Parse Markdown using Parsedown library.
- Valid values:
boolean— if you need to parse with default params, just passtrueobject— or an object with parameters (see below)
- Default value:
false
-
tools->markdownParser->parseInline- Description: Parse only inline elements.
- Valid values:
boolean - Default value:
false
Typographer
-
tools->typographer- Description: Typography text using EvolutionCMS.snippets.ddTypograph.
- Parameters have to be passed to EvolutionCMS.snippets.ddTypograph.
- More info in its documentation.
- Valid values:
boolean— if you need to typography with default params, just passtrueobject— or an object with parameters (see below)
- Default value:
false
- Description: Typography text using EvolutionCMS.snippets.ddTypograph.
-
tools->typographer->optAlign- Description: Optical alignment (hanging punctuation).
- Valid values:
boolean - Default value:
false
-
tools->typographer->optAlign_useClasses- Description: Use CSS classes instead of inline styles for optical alignment (
<span class="oa_comma_b">instead of<span style="margin-right:-0.2em;">).- If the parameter is enabled, don't forget to specify the following CSS rules on your site:
.oa_obracket_sp_s {margin-right:0.3em;} .oa_obracket_sp_b {margin-left:-0.3em;} .oa_obracket_nl_b {margin-left:-0.3em;} .oa_comma_b {margin-right:-0.2em;} .oa_comma_e {margin-left:0.2em;} .oa_oquote_nl {margin-left:-0.44em;} .oa_oqoute_sp_s {margin-right:0.44em;} .oa_oqoute_sp_q {margin-left:-0.44em;}
- If the parameter is enabled, don't forget to specify the following CSS rules on your site:
- Valid values:
01
- Default value:
0
- Description: Use CSS classes instead of inline styles for optical alignment (
-
tools->typographer->text_paragraphs- Description: Section signs and line breaks insertion.
- Valid values:
boolean - Default value:
false
-
tools->typographer->text_autoLinks- Description: Marking links (including email ones).
- Valid values:
boolean - Default value:
false
-
tools->typographer->etc_unicodeConvert- Description: Convert HTML entities into Unicode (
—instead of—, etc). - Valid values:
boolean - Default value:
true
- Description: Convert HTML entities into Unicode (
-
tools->typographer->noTags- Description: Whether HTML element insertion is allowed or not.
- There are cases when using tags causes the text to be invalid, for example, using the snippet inside of an HTML attribute.
- Valid values:
boolean - Default value:
false
- Description: Whether HTML element insertion is allowed or not.
-
tools->typographer->excludeTags- Description: HTML tags which content will be ignored by snippet.
- Valid values:
stringCommaSeparated - Default value:
'notg,code'
Tag remover
-
tools->tagRemover- Description: Strip HTML and PHP tags from a string.
- Valid values:
boolean— if you need to remove all tags, just passtrueobject— or an object with parameters (see below)
- Default value:
false
-
tools->tagRemover->allowed- Description: Use the parameter to specify tags which should not be stripped (e. g.
<p><div>). - Valid values:
string - Default value:
''
- Description: Use the parameter to specify tags which should not be stripped (e. g.
Special char converter
tools->specialCharConverter- Description: Convert special characters to HTML entities.
- Valid values:
boolean - Default value:
false
Chars escaper (e. g. for JS)
-
tools->charEscaper- Description: Escape special characters for JS.
- Valid values:
boolean— if you need to escape with default params, just passtrueobject— or an object with parameters (see below)
- Default value:
false
-
tools->charEscaper->backslashes- Description: Escape backslashes (
'\\'will be replaced to'\\\\'). - Valid values:
boolean - Default value:
true
- Description: Escape backslashes (
-
tools->charEscaper->lineBreaks- Description: Escape line breaks (
"\r\n","\r","\n"will be replaced to'\r\n'). - Valid values:
boolean - Default value:
true
- Description: Escape line breaks (
-
tools->charEscaper->tabs- Description: Escape tabs (
' '(tab) will be replaced to' '(space)). - Valid values:
boolean - Default value:
true
- Description: Escape tabs (
-
tools->charEscaper->modxPlaceholders- Description: Escape (MODX)EvolutionCMS placeholders (
'[+'and'+]'will be replaced to'\[\+'and'\+\]'). - Valid values:
boolean - Default value:
true
- Description: Escape (MODX)EvolutionCMS placeholders (
-
tools->charEscaper->quotes- Description: Escape quotes (
"'"and'"'will be replaced to"\'"and'\"'). - Valid values:
boolean - Default value:
true
- Description: Escape quotes (
URL encoder
tools->urlEncoder- Description: URL-encode according to RFC 3986.
- Valid values:
boolean - Default value:
false
Placeholder remover
tools->placeholderRemover- Description: Remove placeholders like
[+placeholder+]. - Valid values:
boolean - Default value:
false
- Description: Remove placeholders like
Preg replacer
-
tools->pregReplacer- Description: Perform a regular expression search and replace.
- Valid values:
object - Default value: —
-
tools->pregReplacer->pattern- Description: The pattern to search for.
- You can omit
/, the default will be/+/u.
- You can omit
- Valid values:
string - Required
- Description: The pattern to search for.
-
tools->pregReplacer->replacement- Description: The string to replace.
- Valid values:
string - Default value:
''
Numberer
-
tools->numberer- Description: Convert and format numbers.
- Valid values:
object - Default value: —
-
tools->numberer->isFloatAllowed- Description: Float number availability status.
- Valid values:
boolean - Default value:
true
-
tools->numberer->decimalsNumber- Description: Number of chars standing after comma.
- Valid values:
integer0— any
- Default value:
0
-
tools->numberer->isDecimalsFixed- Description: Allows formatting a number using fixed-point notation (e. g.
10.00) according totools->numberer->decimalsNumber. - Valid values:
boolean - Default value:
false
- Description: Allows formatting a number using fixed-point notation (e. g.
-
tools->numberer->thousandsSeparator- Description: Character used to separate thousands (e. g.
' 'for1 234 567or','for1,234,567). - Valid values:
string - Default value:
''
- Description: Character used to separate thousands (e. g.
Tpl parser
-
tools->tplParser- Description: Gets the chunk contents by its name and parse it.
- If
inputStringis empty, the chunk content will not be returned, just an empty string.
- If
- Valid values:
object - Default value: —
- Description: Gets the chunk contents by its name and parse it.
-
tools->tplParser->tpl- Description: Chunk name or code via
@CODE:prefix.- Available placeholders:
[+snippetResult+]— theinputStringmodified by previous tools
- Available placeholders:
- Valid values:
stringChunkNamestring— use inline templates starting with@CODE:
- Required
- Description: Chunk name or code via
-
tools->tplParser->placeholders- Description: Additional data has to be passed into the
tools->tplParser->tpl.- Nested objects and arrays are supported too:
{"someOne": "1", "someTwo": "test" }=>[+someOne+], [+someTwo+].{"some": {"a": "one", "b": "two"} }=>[+some.a+],[+some.b+].{"some": ["one", "two"] }=>[+some.0+],[+some.1+].
- Nested objects and arrays are supported too:
- Valid values:
object - Default value: —
- Description: Additional data has to be passed into the
Examples
All examples are written using HJSON, but if you want you can use vanilla JSON instead.
Convert characters to lowercase (tools->caseConverter->toLower)
[[ddStringTools?
&inputString=`Some STRING with DiFFerEnt case`
&tools=`{
caseConverter: {
toLower: true
}
}`
]]
Returns:
some string with different case
Strip HTML and PHP tags from a string (tools->tagRemover)
Remove all tags completely
[[ddStringTools? &inputString=`<div class="someTrash"></div><p><b>Some</b> <a href="#">sample</a> <i>text</i>.</p>` &tools=`{ tagRemover: true }` ]]
Returns:
Some sample text.
Remove all tags except <p> and <a> (tools->tagRemover->allowed)
If you want to preserve some tags, pass an object with property allowed instead of true.
[[ddStringTools? &inputString=`<div class="someTrash"></div><p><b>Some</b> <a href="#">sample</a> <i>text</i>.</p>` &tools=`{ tagRemover: { allowed: "<p><a>" } }` ]]
Returns:
<p>Some <a href="#">sample</a> text.</p>
Convert special characters to HTML entities (tools->specialCharConverter)
[[ddStringTools? &inputString=`<p>Some <a href="#">sample</a> text.</p>` &tools=`{ specialCharConverter: true }` ]]
Returns:
<p>Some <a href="#">sample</a> text.</p>
URL-encode according to RFC 3986 (tools->urlEncoder)
[[ddStringTools?
&inputString=`tags[]=Maps&tags[]=URLs`
&tools=`{
urlEncoder: true
}`
]]
Returns:
tags%5B%5D%3DMaps%26tags%5B%5D%3DURLs
Escape special characters for JavaScript (tools->charEscaper)
<script> $('body').append('[[ddStringTools? &inputString=` <p class="test">Some <a href="#">sample</a> text.</p> <p>New line.</p> ` &tools=`{ charEscaper: true }` ]]'); <script>
Returns:
<script> $('body').append('\r\n <p class=\"test\">Some <a href=\"#\">sample</a> text.</p>\r\n <p>New line.</p>\r\n '); <script>
Convert Markdown to HTML (tools->markdownParser)
[[ddStringTools?
&inputString=`
# Markdown example
Some text in _Markdown_.
`
&tools=`{
markdownParser: true
}`
]]
Returns:
<h1>Markdown example</h1> <p>Some text in <em>Markdown</em>.</p>
You can also parse inline markdown only (tools->markdownParser->parseInline)
[[ddStringTools?
&inputString=`Some text in _Markdown_.`
&tools=`{
"markdownParser": {
"parseInline": true
}
}`
]]
Returns:
Some text in <em>Markdown</em>.
Typography text (tools->typographer)
With optical alignment (tools->typographer->optAlign)
[[ddStringTools? &inputString=`<p>Some text containing "quoted" text.</p>` &tools=`{ typographer: { optAlign: true } }` ]]
Returns:
<p>Some text containing<span style="margin-right:0.44em;"> </span><span style="margin-left:-0.44em;">“</span>quoted” text.</p>
Simple call with default parameters
[[ddStringTools?
&inputString=`Some text for typography.`
&tools=`{
typographer: true
}`
]]
Remove placeholders like [+placeholder+] (tools->placeholderRemover)
[[ddStringTools?
&inputString=`Some [+thing+] with [+placeholder1+] and [+placeholder2+].`
&tools=`{
placeholderRemover: true
}`
]]
Returns:
Some with and .
Thumbnail suffix (tools->pregReplacer)
[[ddStringTools?
&inputString=`assets/images/someImage.png`
&tools=`{
pregReplacer: {
pattern: (.*)(\.\D*)
replacement: $1_50x50$2
}
}`
]]
Returns:
assets/images/someImage_50x50.png
Numberer (tools->numberer)
Convert string to integer
[[ddStringTools?
&inputString=`42.75`
&tools=`{
numberer: {
isFloatAllowed: false
}
}`
]]
Returns: 42
Convert string to float with maximum 2 decimal places
[[ddStringTools?
&inputString=`42.7589`
&tools=`{
numberer: {
decimalsNumber: 2
}
}`
]]
Returns: '42.76'
Format product price with fixed decimal places
[[ddStringTools?
&inputString=`1999`
&tools=`{
numberer: {
decimalsNumber: 2
isDecimalsFixed: true
}
}`
]]
Returns: '1999.00'
Format large number with space as thousands separator
[[ddStringTools?
&inputString=`1234567`
&tools=`{
numberer: {
thousandsSeparator: ' '
}
}`
]]
Returns: '1 234 567'
Format price with comma as thousands separator and fixed decimal places
[[ddStringTools?
&inputString=`1234567.891`
&tools=`{
numberer: {
decimalsNumber: 2
isDecimalsFixed: true
thousandsSeparator: ','
}
}`
]]
Returns: '1,234,567.89'
Parse price string with currency symbol, spaces and other invalid chars
[[ddStringTools?
&inputString=`$1 234 000.56 lorem ipsum`
&tools=`{
numberer: true
}`
]]
Returns: '1234000.56'
Get the chunk content and pass some placeholders (tools->tplParser)
[[ddStringTools?
&inputString=`Some input string text.`
&tools=`{
tplParser: {
tpl:
'''
@CODE:[+before+]<p>[+snippetResult+]</p>[+after+]
'''
placeholders: {
before:
'''
<p>Some start text.</p>
'''
after:
'''
<p>Some end text.</p>
'''
}
}
}`
]]
Returns:
<p>Some start text.</p><p>Some input string text.</p><p>Some end text.</p>
Use multiple tools together
[[ddStringTools? &inputString=`<div class="someTrash"></div><p><b>Some</b> <a href="#">sample</a> <i>text</i>. [+somePlaceholder+]</p>.` &tools=`{ placeholderRemover: true typographer: true tagRemover: { allowed: "<p><a>" } caseConverter: { toLower: true } charEscaper: true }` ]]
Tools are called in accordance with the specified order:
- First placeholders like
[+somePlaceholder+]will removed, then - Text will typographied,
- All HTML tags except
<p>and<a>will removed, - Text will converted to lowercase,
- And escaped for JS.
Pass inputString as an array through $modx->runSnippet
The input string can also be set as a PHP object or array (e. g. for calls through $modx->runSnippet).
In this case, it will be converted to JSON first.
$modx->runSnippet( 'ddStringTools', [ // This is an array, not string 'inputString' => [ 'someObjectField' => '[+somePlaceholder+] need to be removed.', // And this is an array too 'otherObjectField' => [ 'deepField' => '[+placeholders+] will be removed in any depth.', ], ], 'tools' => [ 'placeholderRemover' => true, ], ] );
Returns:
{
"someObjectField": " need to be removed.",
"otherObjectField": {
"deepField": " will be removed in any depth."
}
}
Run the snippet through \DDTools\Snippet::runSnippet without DB and eval
// Include (MODX)EvolutionCMS.libraries.ddTools require_once( $modx->getConfig('base_path') . 'assets/libs/ddTools/modx.ddtools.class.php' ); // Run (MODX)EvolutionCMS.snippets.ddStringTools \DDTools\Snippet::runSnippet([ 'name' => 'ddStringTools', 'params' => [ 'inputString' => '<div class="someTrash"></div><p><b>Some</b> <a href="#">sample</a> <i>text</i>. [+somePlaceholder+]</p>.', // `tools` in this case can be set as a native PHP array or object 'tools' => [ 'placeholderRemover' => true, 'typographer' => true, 'tagRemover' => [ 'allowed' => '<p><a>', ], 'caseConverter' => [ 'toLower' => true, ], 'charEscaper' => true, ], ], ]);
Links
dd/evolutioncms-snippets-ddstringtools 适用场景与选型建议
dd/evolutioncms-snippets-ddstringtools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「markdown」 「typography」 「strings」 「modx」 「MODX Evo」 「typograph」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dd/evolutioncms-snippets-ddstringtools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dd/evolutioncms-snippets-ddstringtools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dd/evolutioncms-snippets-ddstringtools 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
PHP SVG font parser
Generate pseudo locale string for unicode testing and marking missing strings
Adds more BBCode
Add a typography test page to your silverstripe website / application.
A Twig extension with typography filter
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-05-08