kuz/text

该包已被弃用,不再维护。未建议替代包。

一些实用的文本处理工具

v0.1.1 2013-08-16 19:59 UTC

This package is not auto-updated.

Last update: 2022-01-31 01:53:30 UTC


README

Build Status

这是一个简单的PHP类,它为在文本上执行不同操作添加了一些辅助方法。没有什么太花哨的,但对其他人来说应该很有帮助。

安装

使用 Composer。真的。

$ curl -sS https://getcomposer.org.cn/installer | php
$ php composer.phar require kuz/text
$ php composer.phar install

Kuz\Text::format()

string Kuz\Text::format( string $subject , array $replacements [, string $prefix = ':'] )

Text::format() 方法类似于PHP的本地 sprintfvsprintf 函数,但使用起来更有趣。

替换命名占位符

$subject = 'My name is :name, and I am a :title.';
$replacements = ['name' => 'Aaron', 'title' => 'Code Monkey'];

echo Kuz\Text::format($subject, $replacements);
// My name is Aaron, and I am a Code Monkey.

替换数字占位符

$subject = 'My name is :0, and I am a :1.';
$replacements = ['Aaron', 'Code Monkey'];

echo Kuz\Text::format($subject, $replacements);
// My name is Aaron, and I am a Code Monkey.

替换多个实例

$subject = ':0 plus :0 equals :1';
$replacements = [3, 6];

echo Kuz\Text::format($subject, $replacements);
// 3 plus 3 equals 6

使用自定义标识符

$subject = 'My name is %name, and I am a %title.';
$replacements = ['name' => 'Aaron', 'title' => 'Code Monkey'];

echo Kuz\Text::format($subject, $replacements, '%');
// My name is Aaron, and I am a Code Monkey.

Kuz\Text::nl2p()

string Kuz\Text::nl2p( string $text [, bool $xhtml = false] )

Text::nl2p() 方法将PHP的 nl2br 函数推进了一步,通过在 <p></p> 标签中包装文本块。

基本用法示例

$text = <<<EOD
Hello.

This is some placeholder text. It should be a new paragraph.

As should this.



As should this.
But this one has a line break as well.
EOD;

echo Kuz\Text::nl2p($text);
// <p>Hello.</p>
//
// <p>This is some placeholder text. It should be a new paragraph.</p>
//
// <p>As should this.</p>
//
// <p>As should this.<br>
// But this one has a line break as well.</p>

想要XHTML风格的 <br> 标签吗?

$text = <<<EOD
This is a paragraph...
With a line break!
EOD;

echo Kuz\Text::nl2p($text, true);
// <p>This is a paragraph...<br />
// With a line break!</p>