tylercd100/laravel-placeholders

在字符串中查找和替换占位符

1.0.0 2017-03-01 18:19 UTC

This package is auto-updated.

Last update: 2024-09-17 14:45:44 UTC


README

Latest Version Software License Build Status Scrutinizer Code Quality Code Coverage Dependency Status Total Downloads

安装

通过 composer 安装 - 在终端中

composer require tylercd100/laravel-placeholders

现在将以下内容添加到 providers 数组中的 config/app.php

Tylercd100\Placeholders\ServiceProvider::class

并将以下内容添加到 aliases 数组中的 config/app.php

"Placeholders" => Tylercd100\Placeholders\Facades\Placeholders::class,

然后您需要在终端中运行以下命令以复制配置文件

php artisan vendor:publish --provider="Tylercd100\Placeholders\ServiceProvider"

用法

use Placeholders;

// Basic
Placeholders::parse("I like [fruit]s and [veg]s", [
	'fruit' => 'orange',
	'veg' => 'cucumber'
]); //I like oranges and cucumbers

// Globally
Placeholders::set("fruit", "apple");
Placeholders::set("veg", "carrot");
Placeholders::parse("I like [fruit]s and [veg]s"); // I like apples and carrots

样式

// Change the style
Placeholders::setStyle("{{", "}}");
Placeholders::parse("I like {{fruit}}s and {{veg}}s", [
	'fruit' => 'lemon',
	'veg' => 'string bean'
]); //I like lemons and string beans

错误

// Throw an error if one is missed
Placeholders::setThorough(true) // This is the default
Placeholders::parse("I like [fruit]s and [veg]s", [
	'fruit' => 'orange',
]); //Throws an Exception: Could not find a replacement for [veg]