定制 andydune/string-replace 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andydune/string-replace

Composer 安装命令:

composer require andydune/string-replace

包简介

Replace markers in string with data.

关键字:

README 文档

README

Build Status Software License Packagist Version Total Downloads

It replace in given string meta data with real data.

Requirements

PHP version >= 7.2

Installation

Installation using composer:

composer require andydune/string-replace

Or if composer was not installed globally:

php composer.phar require andydune/string-replace

Or edit your composer.json:

"require" : {
     "andydune/string-replace": "^1"
}

And execute command:

php composer.phar update

SimpleReplace

It's very simple and lightweight replace methods. It uses str_replace function.

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' 

There is no any logic in it and it will no replace statements if no data to replace.

PowerReplace

It powerful replace class with string analytics with regular. There are many functions built-in lib and you may add custom easily.

No case sensitive

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' 

Functions

Functions are described next to marker after : (you can change separator).

Functions can get parameters: #CODE:maxlen(10)# or #CODE:maxlen("10")#

Symbols: : ( ) , " ' are reserved to use as parameters for function. So if you want to use it you mast encase it with quotes (or single quotes).

This is correct usage:

$string = "Params: #weight:prefix(\"'\"):postfix('"')#";
$string = "Params: #weight:prefix(\":\"):postfix(':')#";
$string = "Params: #weight:prefix(\"(\"):postfix(')')#";
$string = "Params: #weight:prefix(\", \"):postfix(', ')#";

More then one function : #CODE:maxlen(10):escape#

escape

Apply htmlspecialchars with inserted value.

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 &lt;b&gt;one_ok&lt;/b&gt;'

addcomma

It adds comma before inserted value if it is not empty.

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 function may get params: comma(param1, param2)

  • param1 set to 1 if you want to miss first comma appearance in string
  • param2 set to 1 if you want to begin new group of words for next missing of first comma appearance in string
$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

Replace marker with value if string behind this one is less then poined in parameter.

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

Print formatted string if it is not empty.

$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

Pluralize the title for number.

$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

Russian pluralize the title for number.

$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

It shows given string as prefix only if value behind the key is not empty.

$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

It shows given string as postfix only if value behind the key is not empty.

$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

It shows string given in second param if first param is equal to value behind the placeholder.

$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

It shows string value behind the current placeholder if another is not empty.

$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

Custom Functions

You can add your own functions with replace rules. Markers and functions are not case sensitive.

$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_?

Application

andydune/string-replace 适用场景与选型建议

andydune/string-replace 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 468 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「array」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 andydune/string-replace 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andydune/string-replace 我们能提供哪些服务?
定制开发 / 二次开发

基于 andydune/string-replace 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 468
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-27