php text 处理,Text: 一个简单的文本处理库

Text - Simple 1 Class Text Manipulation Library

68747470733a2f2f706f7365722e707567782e6f72672f6b7a796b6879732f746578742f762f737461626c652e706e67e7e6f80eea9db8386ed533555efe639f.png25f0bd66d428c6d9928446e495885a34.png

Do you remember PHP's string functions? If not, just wrap you text with Text! It will save a minute on your coding.

Text is extracted from kzykhys/Ciconia. this is used for markdown processing.

Installation

Modify your composer.json and run php composer.phar update

{

"require": {

"kzykhys/text":"~1.0.0"

}

}

Requirements

PHP5.4+

Get Started

Text acts like a string

use KzykHys\Text\Text;

$text = new Text('Lorem Ipsum');

echo $text;

// Lorem Ipsum

Text can also be called statically

use KzykHys\Text\Text;

$text = Text::create('Lorem Ipsum');

echo $text;

// Lorem Ipsum

Manipulation methods are chainable:

$text = new Text('foo');

$text

->append('bar') // foobar

->prepend('baz') // bazfoobar

->wrap('-') // -bazfoobar-

->upper() // -BAZFOOBAR-

->lower() // -bazfoobar-

->trim('-') // bazfoobar

->rtrim('r') // bazfooba

->ltrim('b') // azfooba

;

Special note for replace()

$text = new Text('FooBarBaz');

$text->replace('/Foo(Bar)(Baz)/', function (Text $whole, Text $bar, Text $baz) {

return $bar->upper()->append($baz->lower());

});

echo $text;

// BARbaz

If the second argument is callable, callback takes at least one parameter. The whole match being first, and matched subpatterns. All parameters are Text instance.

API

Manipulation (Chainable)

Method

Description

create($text)

Create a new Text instance.

append($text)

Append the string.

prepend($text)

Prepend the string.

wrap($start, [$end])

Surround text with given string.

lower()

Make a string lowercase.

upper()

Make a string uppercase.

trim([$charList])

Strip whitespace (or other characters) from the beginning and end of a string.

rtrim([$charList])

Strip whitespace (or other characters) from the end of a string.

ltrim([$charList])

Strip whitespace (or other characters) from the beginning of a string.

escapeHtml([$option])

Convert special characters to HTML entities.

replace($pattern, $replacement)

Perform a regular expression search and replace. If $replacement is the callable, a callback that will be called and passed an array of matched elements in the subject string.

replaceString($search, $replace)

Replace all occurrences of the search string with the replacement string.

indent([$spaces])

Add one level of line-leading spaces.

outdent([$spaces])

Remove one level of line-leading tabs or spaces.

detab([$spaces])

Convert tabs to spaces.

eachLine($callback)

Apply a user function to every line of the string.

Test

Method

Description

isEmpty()

Determine whether a variable is empty

isNumeric()

Finds whether a variable is a number or a numeric string

match($pattern, [&$matches])

Perform a regular expression match

Miscellaneous

Method

Description

split($pattern, [$flags])

Split string by a regular expression

lines([$pattern])

Split string by a line break

chars()

Convert a string to an array

length()

Gets the length of a string

countLines()

Gets the number of lines

indexOf($needle, [$offset])

Find the position of the first occurrence of a substring in a string

Filesystem

Method

Description

save($path)

Write a string to a file

License

The MIT License

Author

Kazuyuki Hayashi (@kzykhys)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值