robclancy/string

本包最新版本(dev-master)没有提供许可信息。

一个PHP库,通过类似其他语言的字符串对象来操作字符串

dev-master 2013-04-09 01:42 UTC

This package is auto-updated.

Last update: 2024-09-22 03:54:49 UTC


README

一个PHP库,通过类似其他语言的字符串对象来操作字符串。

这个库旨在提供一种替代方法,以使用PHP的不一致字符串函数,而不必依赖于简单的包装器。基本上是一个类似于其他语言的字符串对象。还包含一个str函数,使操作更简洁、更方便,并在大量处理字符串时隐藏较长的new String

本库有两个依赖项:oodle/inflectpatchwork/utf8

安装和设置

要安装,请将以下内容添加到您的 composer.json 文件中

"robclancy/string": "dev-master"

然后您可以直接使用 RobClancy\String\Stringstr。或者创建一个别名,如下所示...

本地

class_alias('RobClancy\String\String', 'String');

Laravel,将其添加到 app/config/app.php 中的别名数组

'String' => 'RobClancy\String\String',

示例说明:其中一些功能尚未实现,本包将在我编写所有测试后才能使用

类名到表名的映射

class UserGroup {
	
	public function getTable()
	{
		// We might want to point to the plural, snake case version of this class
		$class = new String(__CLASS__);

		// Snake case and split
		$words = $class->snake()->split('_');

		// Pluralize last word
		// Note: at a later stage I might have an array object which will be used here to do $words->last()->plural();
		$words[count($word)-1]->plural();

		// Now return it joined back up
		return String::join($words, '_');
	}
}

Ruby风格的字符串替换和Python风格的切片

$string = new String('Jason made Basset, it is pretty cool I hear, vote 1 Jason!!');

// String replace, the same as doing the key as $search and the value as $replace in $string->replace($search, $value)
$string['Jason'] = 'Jason Lewis';
$string['Basset'] = 'Basset (Better Asset Management)';

// We now want to change the 1 into 9001 but because the array notation here is overloaded to do python style slicing
// and ruby style replacing we need to force it to the replace, we do this simply by starting the replace with 'r|'
$string['r|1'] = 9001;

// Lastly let's clean it up and make it end with a single !
$string->finish('!');
// or
$string['!!'] = '!';
// or
$string->slice(0, -1);
// or the same as above with python syntax.
$string = $string[':-1'];

echo $string;
// Outputs: "Jason Lewis made Basset (Better Asset Management), it is pretty cool I hear, vote 9001 Jason Lewis!"

// Just another example of slicing with python
$string = new String('I like pizza :D');
$pizza = $string['7:-3'];
echo $pizza; // pizza

基本的快速验证,使用异常

$string = 'Love for laravel <3';
$string->startsWith('Love');	// true
$string->contains('something'); // false
$string->endsWith('<3');		// true
$string->is('No love for laravel'); // obviously returns false!

// Now to show with and without exceptions
$string = new String('not_an_email');
$string->isEmail(); // This will return false

$string->useExceptions(true);
$string->isEmail(); // This will now throw an exception

// But calling that method is too verbose, so you can use a shortcut on string creation by passing true as the second argument
$string = new String('still not an email', true);

// Now any check will throw an exception so you can do quick checking and chain it like the following
try
{
	// String must be an email to do with gmail and contain the word awesome
	$string->isEmail()->endsWith('@gmail.com')->contains('awesome');
}
catch (StringException $e) // TODO: change this to whatever I call the exceptions
{
	// failed
}

// Also you can globally set the exceptions flag to be used if one is not specified, defaults to false
String::throwExceptions(true);

迭代

$string = new String('It\'s Saturday, I shouldn\'t be working on this and drinking or something');

// You can loop over the string chracter by character
// Let's make the first letter of each word a capital just 'cause
$previousSpace = false;
foreach ($string AS $offset => $char)
{
	if ($char->is(' '))
	{
		$previousSpace = true;
		continue;
	}

	if ($previousSpace)
	{
		$string[$offset] = $char->upper();
	}

	$previousSpace = false;
}

echo $string; // It\'s Saturday, I Shouldn\'t Be Working On This And Drinking Or Something

// We can do your usual splits, however in this case it splits into String objects like you would expect
$words = $string->split(' '); // normal array

// Now let's do the same change as above but instead on each word, easier this time
foreach ($words AS $key => $word)
{
	$words[$key] = $word->upperFirst();
}

// Basically an alias for implode here
$string = String::join($words, ' ');
echo $string; // It\'s Saturday, I Shouldn\'t Be Working On This And Drinking Or Something

Build Status