andydune / string-replace
在字符串中用数据替换标记。
v1.10.1
2018-08-10 09:57 UTC
Requires
- php: >=5.6
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^5.7.15 || ^6.0.8
README
它将在给定字符串中使用实际数据替换元数据。
要求
PHP版本 >= 7.2
安装
使用composer安装
composer require andydune/string-replace
如果全局未安装composer
php composer.phar require andydune/string-replace
或编辑你的 composer.json
"require" : {
"andydune/string-replace": "^1"
}
并执行以下命令
php composer.phar update
SimpleReplace
这是一个非常简单且轻量级的替换方法。它使用 str_replace 函数。
use AndyDune\StringReplace\SimpleReplace; $instance = new SimpleReplace(); $instance->one = 'one_ok'; $instance->two = 'two_ok'; $string = 'Gogoriki go #one# and #two#'; $instance->replace($string); // equals to 'Gogoriki go one_ok and two_ok'
其中没有任何逻辑,如果没有要替换的数据,则不会替换语句。
PowerReplace
这是一个强大的替换类,它具有基于正则表达式的字符串分析。库中内置了许多函数,你可以轻松地添加自定义函数。
不区分大小写
use AndyDune\StringReplace\PowerReplace; $instance = new PowerReplace(); $instance->one = 'one_ok'; $instance->TWO = 'two_ok'; // upper key $string = 'Gogoriki go #ONE# and #two#'; $instance->replace($string); // equals to 'Gogoriki go one_ok and two_ok'
函数
函数在标记之后描述(你可以更改分隔符)。
函数可以获取参数:#CODE:maxlen(10)# 或 #CODE:maxlen("10")#
符号: : ( ) , " ' 是作为函数参数保留的。因此,如果你想使用它,必须用引号(或单引号)括起来。
这是正确的用法
$string = "Params: #weight:prefix(\"'\"):postfix('"')#"; $string = "Params: #weight:prefix(\":\"):postfix(':')#"; $string = "Params: #weight:prefix(\"(\"):postfix(')')#"; $string = "Params: #weight:prefix(\", \"):postfix(', ')#";
多个函数: #CODE:maxlen(10):escape#
escape
应用 htmlspecialchars 到插入的值。
use AndyDune\StringReplace\PowerReplace; $string = 'Gogoriki go #ONE:escape#'; $instance = new PowerReplace(); $instance->one = '<b>one_ok</b>'; $instance->replace($string); // equals to 'Gogoriki go <b>one_ok</b>'
addcomma
如果插入的值不为空,则在其前面添加逗号。
use AndyDune\StringReplace\PowerReplace; $string = 'Gogoriki go #one##two:comma#'; $instance = new PowerReplace(); $instance->one = 'swim'; $instance->one = 'play'; $instance->replace($string); // equals to 'Gogoriki go swim, play' $string = 'Gogoriki go #one##two:comma#'; $instance = new PowerReplace(); $instance->one = 'swim'; $instance->replace($string); // equals to 'Gogoriki go swim
comma 函数可以获取参数:comma(param1, param2)
- param1 设置为
1如果你想忽略字符串中的第一个逗号出现 - param2 设置为
1如果你想在字符串中下一次缺少第一个逗号出现时开始新的单词组
$string = 'I know words: #it:addcomma(1)##and_it:addcomma(1)# and #and_it_2:addcomma(1, 1)#'; $instance = new PowerReplace(); $instance->setArray([ 'it' => 'eat', 'and_it' = 'play', 'and_it_2' = 'sleep' ]); $instance->replace($string); // equals to 'I know words: eat, play and sleep'
maxlen
如果此标记后面的字符串长度小于参数中指定的长度,则用值替换标记。
use AndyDune\StringReplace\PowerReplace; $string = 'Gogoriki go #one##two:masxlen(5):addcomma#'; $instance = new PowerReplace(); $instance->one = 'swim'; $instance->one = 'play'; $instance->replace($string); // equals to 'Gogoriki go swim, play' $instance->one = 'swim'; $instance->one = 'play games'; $instance->replace($string); // equals to 'Gogoriki go swim'
printf
如果它不为空,则打印格式化的字符串。
$string = 'I know words: #it:printf(«%s»):addcomma(1)##and_it:printf(«%s»):addcomma(1)# and #and_it_2:printf(«%s»):addcomma(1, 1)#'; $instance = new PowerReplace(); $instance->it = 'eat'; $instance->and_it_2 = 'sleep'; $instance->replace($string); // equals to I know words: «eat» and «sleep»
plural
为数字复数化标题。
$string = 'I see #count# #count:plural(man, men)#'; $instance = new PowerReplace(); $instance->count = 1; $instance->replace($string); // I see 1 man $instance->count = 21; $instance->replace($string); // I see 21 men
pluralrus
为数字俄语复数化标题。
$string = 'У меня есть #count# #count:pluralrus(яблоко, яблока, яблок)#'; $instance = new PowerReplace(); $instance->count = 1; $instance->replace($string)); // У меня есть 1 яблоко $instance->count = 21; $instance->replace($string); // У меня есть 21 яблоко $instance->count = 2; $instance->replace($string); // У меня есть 2 яблока $instance->count = 5; $instance->replace($string); // У меня есть 5 яблок
prefix
如果键后面的值不为空,则仅显示给定的字符串作为前缀。
$string = 'Vegetables I have: #apple_count:prefix("apples "):addcomma(1)##orange_count:prefix("oranges "):addcomma(1)#'; $instance = new PowerReplace(); $instance->apple_count = 1; $instance->replace($string); // Vegetables I have: apples 1
postfix
如果键后面的值不为空,则仅显示给定的字符串作为后缀。
$string = 'Params: #weight:prefix("weight: "):postfix(kg)##growth:prefix("growth: "):postfix(sm):addcomma#'; $instance = new PowerReplace(); $instance->weight = 80; $instance->growth = 180; $instance->replace($string); // Params: weight: 80kg, growth: 180sm
showIfEqual
如果第一个参数等于占位符后面的值,则显示第二个参数中的字符串。
$string = 'Anton #weight:showIfEqual(80, "has normal weight")##weight:showIfEqual(180, "has obesity")#.'; $instance = new PowerReplace(); $instance->weight = 80; $instance->replace($string); // Anton has normal weight. $string = 'Anton #weight:showIfEqual(80, "has normal weight")##weight:showIfEqual(180, "has obesity")#.'; $instance = new PowerReplace(); $instance->weight = 180; $instance->replace($string); // Anton has obesity.
showIfOtherValueNotEmpty
如果另一个不为空,则显示当前占位符后面的字符串值。
$string = 'Variants #type[name]:showIfOtherNotEmpty(type[value])##type[value]:prefix(": ")#'; $instance = new PowerReplace(); $instance->setArray(['type'=> ['name' => 'color', 'value' => 'green']]); $instance->replace($string); // Variants color: green
自定义函数
你可以添加自己的函数,并使用替换规则。标记和函数不区分大小写。
$string = 'Where is #word:leftAndRight(_)#?'; // or the same $string = 'Where is #WORD:LEFTANDRIGHT(_)#?'; $functionHolder = new FunctionsHolder(); // add custom function with name leftAndRight $functionHolder->addFunction('leftAndRight', function ($string, $symbol = '') { return $symbol . $string . $symbol; }); $instance = new PowerReplace($functionHolder); $instance->word = 'center'; $instance->replace($string); // Where is _center_?