jderusse/format

简单的字符串格式化函数

1.0.0 2016-12-16 08:49 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:12 UTC


README

# 格式化

Format 提供变量替换的能力。

## 安装

composer require jderusse/format

用法

format('Hello {who}', ['who' => 'World']);

用例

throw new \Exception(format('Fail to load resource "{resource}".', ['resource' => $fileName]);
$this->addFlash(format('Project "{name}" succesfully created.', ['name' => $project->getName()]);

为什么?

因为我对语言替代方案不满意。

- echo 'Should I use "'."'".'" or "'.'"'.'"';
+ echo format('Should I use "{single}" or "{double}"', ['single'=> "'", 'double' => '"']);
- sprintf('%s likes when %s repeat the same things', $user, $user');
+ format('{user} like when {user} repeat the same things', ['user'=> $user]);
- sprintf('%s%s%s', $scheme, $host, $path');
+ format('{scheme}{host}{path}', ['scheme' => $scheme, 'host' => $host, 'path' => $path]);
- echo "Hello {$this->getUser()->getusername()}. Welcome in {$this->getProject()->getName()}":
+ echo format('Hello {user}. Welcome in {project}', ['user' => $this->getUser()->getusername(), 'project' => $this->getProject()->getName()]):