jderusse/format

Simple function to format strings

1.0.0 2016-12-16 08:49 UTC

This package is auto-updated.

Last update: 2024-10-29 04:31:11 UTC


README

# Format

Format provide the ability to do variable substitutions.

## Installation

composer require jderusse/format

Usage

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

Usecase

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

Why?

Because I'm not happy with language alternatives.

- 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()]):