辅助函数¶
概览¶
Phalcon\Support\HelperFactory提供用于操作数组、文件、JSON、数字和字符串的辅助方法。该工厂替代了Phalcon\Text
组件,提供了相同的功能以及更多功能。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
```
The methods are available either by calling `newInstance()` on the factory object with the relevant name of the helper class or by calling the helper class directly as a method on the helper factory. The factory acts as a service locator, caching the objects to be reused if need be, in the same request.
## Available Classes
| Type | Name | Class |
|---------|-----------------|--------------------------------------------|
| Array | `blacklist` | `Phalcon\Support\Helper\Arr\Blacklist` |
| Array | `chunk` | `Phalcon\Support\Helper\Arr\Chunk` |
| Array | `filter` | `Phalcon\Support\Helper\Arr\Filter` |
| Array | `first` | `Phalcon\Support\Helper\Arr\First` |
| Array | `firstKey` | `Phalcon\Support\Helper\Arr\FirstKey` |
| Array | `flatten` | `Phalcon\Support\Helper\Arr\Flatten` |
| Array | `get` | `Phalcon\Support\Helper\Arr\Get` |
| Array | `group` | `Phalcon\Support\Helper\Arr\Group` |
| Array | `has` | `Phalcon\Support\Helper\Arr\Has` |
| Array | `isUnique` | `Phalcon\Support\Helper\Arr\IsUnique` |
| Array | `last` | `Phalcon\Support\Helper\Arr\Last` |
| Array | `lastKey` | `Phalcon\Support\Helper\Arr\LastKey` |
| Array | `order` | `Phalcon\Support\Helper\Arr\Order` |
| Array | `pluck` | `Phalcon\Support\Helper\Arr\Pluck` |
| Array | `set` | `Phalcon\Support\Helper\Arr\Set` |
| Array | `sliceLeft` | `Phalcon\Support\Helper\Arr\SliceLeft` |
| Array | `sliceRight` | `Phalcon\Support\Helper\Arr\SliceRight` |
| Array | `split` | `Phalcon\Support\Helper\Arr\Split` |
| Array | `toObject` | `Phalcon\Support\Helper\Arr\ToObject` |
| Array | `validateAll` | `Phalcon\Support\Helper\Arr\ValidateAll` |
| Array | `validateAny` | `Phalcon\Support\Helper\Arr\ValidateAny` |
| Array | `whitelist` | `Phalcon\Support\Helper\Arr\Whitelist` |
| File | `basename` | `Phalcon\Support\Helper\File\Basename` |
| JSON | `decode` | `Phalcon\Support\Helper\Json\Decode` |
| JSON | `encode` | `Phalcon\Support\Helper\Json\Encode` |
| Number | `isBetween` | `Phalcon\Support\Helper\Number\IsBetween` |
| String | `camelize` | `Phalcon\Support\Helper\Str\Camelize` |
| String | `concat` | `Phalcon\Support\Helper\Str\Concat` |
| String | `countVowels` | `Phalcon\Support\Helper\Str\CountVowels` |
| String | `decapitalize` | `Phalcon\Support\Helper\Str\Decapitalize` |
| String | `decrement` | `Phalcon\Support\Helper\Str\Decrement` |
| String | `dirFromFile` | `Phalcon\Support\Helper\Str\DirFromFile` |
| String | `dirSeparator` | `Phalcon\Support\Helper\Str\DirSeparator` |
| String | `dynamic` | `Phalcon\Support\Helper\Str\Dynamic` |
| String | `endsWith` | `Phalcon\Support\Helper\Str\EndsWith` |
| String | `firstBetween` | `Phalcon\Support\Helper\Str\FirstBetween` |
| String | `friendly` | `Phalcon\Support\Helper\Str\Friendly` |
| String | `humanize` | `Phalcon\Support\Helper\Str\Humanize` |
| String | `includes` | `Phalcon\Support\Helper\Str\Includes` |
| String | `increment` | `Phalcon\Support\Helper\Str\Increment` |
| String | `interpolate` | `Phalcon\Support\Helper\Str\Interpolate` |
| String | `isAnagram` | `Phalcon\Support\Helper\Str\IsAnagram` |
| String | `isLower` | `Phalcon\Support\Helper\Str\IsLower` |
| String | `isPalindrome` | `Phalcon\Support\Helper\Str\IsPalindrome` |
| String | `isUpper` | `Phalcon\Support\Helper\Str\IsUpper` |
| String | `kebabCase` | `Phalcon\Support\Helper\Str\KebabCase` |
| String | `len` | `Phalcon\Support\Helper\Str\Len` |
| String | `lower` | `Phalcon\Support\Helper\Str\Lower` |
| String | `pascalCase` | `Phalcon\Support\Helper\Str\PascalCase` |
| String | `prefix` | `Phalcon\Support\Helper\Str\Prefix` |
| String | `random` | `Phalcon\Support\Helper\Str\Random` |
| String | `reduceSlashes` | `Phalcon\Support\Helper\Str\ReduceSlashes` |
| String | `snakeCase` | `Phalcon\Support\Helper\Str\SnakeCase` |
| String | `startsWith` | `Phalcon\Support\Helper\Str\StartsWith` |
| String | `suffix` | `Phalcon\Support\Helper\Str\Suffix` |
| String | `ucwords` | `Phalcon\Support\Helper\Str\Ucwords` |
| String | `uncamelize` | `Phalcon\Support\Helper\Str\Uncamelize` |
| String | `underscore` | `Phalcon\Support\Helper\Str\Underscore` |
| String | `upper` | `Phalcon\Support\Helper\Str\Upper` |
```php
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$upper = $helper->newInstance('upper');
方法¶
方法可以直接从助手工厂调用。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'abcde';
$result = $helper->upper($source);
echo $result; // ABCDE
blacklist
¶
blacklist(array $collection, array $blackList): array
通过从黑名单中的元素获得的键来排除数组的元素
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'value-1',
'key-2' => 'value-2',
'key-3' => 'value-3',
9 => 'value-4',
12 => 'value-5',
' key-6 ' => 'value-6',
99 => 'value-7',
'key-8' => 'value-8',
];
$blackList = [
99,
48,
31,
9,
'key-45',
null,
-228,
new stdClass(),
[],
3.501,
false,
'key-2',
'key-3',
];
$result = $helper->blacklist($source, $blackList);
var_dump($result);
// [
// 'value-1',
// 12 => 'value-5',
// ' key-6 ' => 'value-6',
// 'key-8' => 'value-8',
// ]
chunk
¶
chunk(array $collection, int $size, bool $preserveKeys = false): array
将数组拆分为指定大小的较小数组
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'k1' => 1,
'k2' => 2,
'k3' => 3,
'k4' => 4,
'k5' => 5,
'k6' => 6,
];
$result = $helper->chunk($source, 2, true);
var_dump($result);
// [
// ['k1' => 1, 'k2' => 2],
// ['k3' => 3, 'k4' => 4],
// ['k5' => 5, 'k6' => 6],
// ]
filter
¶
filter(array $collection, mixed $method = null): mixed
使用 array_filter 对集合进行过滤,并在定义了可调用对象时使用它。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
];
$result = $helper->filter(
$source,
function ($element) {
return $element & 1;
}
);
var_dump($result);
// [
// 1 => 1,
// 3 => 3,
// 5 => 5,
// 7 => 7,
// 9 => 9,
// ]
first
¶
first(array $collection, callable $method = null): mixed
返回集合的第一个元素。如果传递了callable
参数,则返回第一个通过验证的元素的键true
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'Phalcon',
'two' => 'Framework',
];
$result = $helper->first($source);
echo $result; // 'Phalcon'
firstKey
¶
firstKey(array $collection, callable $method = null): mixed
返回集合中第一个元素的键。如果传递了callable
参数,则返回第一个通过验证的元素的键true
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'Phalcon',
'two' => 'Framework',
];
$result = $helper->firstKey($source);
echo $result; // 'one'
flatten
¶
flatten(array $collection, bool $deep = false): array
将数组扁平化到一个层级深度,除非另有说明。$deep
设置为`true`
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [1, [2], [[3], 4], 5];
$result = $helper->flatten($source);
var_dump($result);
// [1, 2, [3], 4, 5]
get
¶
get(array $collection, mixed $index, mixed $defaultValue = null, string $cast = null): mixed
通过键获取数组元素,如果不存在则返回默认值。它还允许使用特定类型对返回值进行强制转换。settype
内部使用
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'Phalcon',
'two' => '1',
];
echo $helper->get($source, 1); // 'Phalcon'
echo $helper->get($source, 3, 'Unknown'); // 'Unknown'
echo $helper->get($source, 2, null, 'int'); // 1
group
¶
group(array $collection, mixed $method): array
根据传入的可调用对象对数组元素进行分组
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
[
'name' => 'Paul',
'age' => 34,
],
[
'name' => 'Peter',
'age' => 31,
],
[
'name' => 'John',
'age' => 29,
],
];
$result = $helper->group($source, 'age');
// [
// 34 => [
// [
// 'name' => 'Paul',
// 'age' => 34,
// ],
// ],
// 31 => [
// [
// 'name' => 'Peter',
// 'age' => 31,
// ],
// ],
// 29 => [
// [
// 'name' => 'John',
// 'age' => 29,
// ],
// ],
// ]
has
¶
has(array $collection, mixed $index): bool
检查数组是否具有特定键的元素并返回结果。true
/false
根据情况返回相应的布尔值。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
1 => 'Phalcon',
'two' => 'Framework',
];
echo $helper->has($source, 1); // true
echo $helper->get($source, 'two'); // true
echo $helper->get($source, 'unknown'); // false
isUnique
¶
isUnique(array $collection): bool
检查数组是否存在重复值。若所有值都唯一则返回true
true,false
。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'Phalcon',
'Framework',
];
echo $helper->isUnique($source); // true
$source = [
'Phalcon',
'Framework',
'Phalcon',
];
echo $helper->isUnique($source); // false
last
¶
last(array $collection, callable $method = null): mixed
返回集合的最后一个元素。如果传递了callable
参数,则返回第一个通过验证的元素的键true
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'Phalcon',
'two' => 'Framework',
];
$result = $helper->last($source);
echo $result; // 'Framework'
lastKey
¶
lastKey(array $collection, callable $method = null): mixed
返回集合中第一个元素的键。如果传递了callable
参数,则返回第一个通过验证的元素的键true
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'Phalcon',
'two' => 'Framework',
];
$result = $helper->lastKey($source);
echo $result; // 'two'
order
¶
order(array $collection, mixed $attribute, string $order = 'asc'): array
按对象的属性对数组或对象集合进行排序。它支持升序/降序排序,以及与ksort
和krsort
<?php
use Phalcon\Support\Helper\Arr\Order;
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
[
'id' => 2,
'name' => 'Paul',
],
[
'id' => 3,
'name' => 'Peter',
],
[
'id' => 1,
'name' => 'John',
],
];
$result = $helper->order($source, 'id');
var_dump($result);
// [
// [
// 'id' => 1,
// 'name' => 'John',
// ],
// [
// 'id' => 2,
// 'name' => 'Paul',
// ],
// [
// 'id' => 3,
// 'name' => 'Peter',
// ],
// ]
$result = $helper->order($source, 'id', Order::ORDER_DESC);
var_dump($result);
// [
// [
// 'id' => 3,
// 'name' => 'Peter',
// ],
// [
// 'id' => 2,
// 'name' => 'Paul',
// ],
// [
// 'id' => 1,
// 'name' => 'John',
// ],
// ]
$source = [
(object) [
'id' => 2,
'name' => 'Paul',
],
(object) [
'id' => 3,
'name' => 'Peter',
],
(object) [
'id' => 1,
'name' => 'John',
],
];
$result = $helper->order($source, 'id');
var_dump($result);
// [
// (object) [
// 'id' => 1,
// 'name' => 'John',
// ],
// (object) [
// 'id' => 2,
// 'name' => 'Paul',
// ],
// (object) [
// 'id' => 3,
// 'name' => 'Peter',
// ],
// ]
pluck
¶
pluck(array $collection, string $element): array
根据集合值返回子集
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
['product_id' => 'prod-100', 'name' => 'Desk'],
['product_id' => 'prod-200', 'name' => 'Chair'],
];
$result = $helper->pluck($source, 'name');
var_dump($result);
// [
// 'Desk',
// 'Chair',
// ]
$source = [
(object) ['product_id' => 'prod-100', 'name' => 'Desk'],
(object) ['product_id' => 'prod-200', 'name' => 'Chair'],
];
$result = $helper->pluck($source, 'name');
var_dump($result);
// [
// 'Desk',
// 'Chair',
// ]
set
¶
set(array $collection, mixed $value, mixed $index = null): array
使用可选键设置数组元素
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'Phalcon',
];
$result = $helper->set($source, 'Framework');
var_dump($result);
// [
// 'one' => 'Phalcon',
// 1 => 'Framework',
// ]
$result = $helper->set($source, 'abcde', 'suffix');
var_dump($result);
// [
// 'one' => 'Phalcon',
// 1 => 'Framework',
// 'suffix' => 'abcde',
// ]
sliceLeft
¶
sliceLeft(array $collection, int $elements = 1): array
返回一个新数组,其中n
元素从左边被移除。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'Phalcon',
'Framework',
'for',
'PHP',
];
$result = $helper->sliceLeft($source);
var_dump($result);
// [
// 'Phalcon',
// ]
$result = $helper->sliceLeft($source, 3);
var_dump($result);
// [
// 'Phalcon',
// 'Framework',
// 'for',
// ]
sliceRight
¶
sliceRight(array $collection, int $elements = 1): array
返回一个新数组,其中n
元素从右边被移除。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'Phalcon',
'Framework',
'for',
'PHP',
];
$result = $helper->sliceRight($source);
var_dump($result);
// [
// 'PHP',
// ]
$result = $helper->sliceRight($source, 3);
var_dump($result);
// [
// 'Framework',
// 'for',
// 'PHP',
// ]
split
¶
split(array $collection): array
返回一个新数组,其中集合的键作为一个元素,值作为另一个元素。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
1 => 'Phalcon',
3 => 'Framework',
];
$result = $helper->split($source);
var_dump($result);
// [
// [1, 3],
// ['Phalcon', 'Framework'],
// ];
toObject
¶
toObject(array $collection): object
将传入的数组作为对象返回
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'two',
'three' => 'four',
];
$result = $helper->toObject($source);
var_dump($result);
// class stdClass#1 (2) {
// public $one =>
// string(3) "two"
// public $three =>
// string(4) "four"
// }
validateAll
¶
validateAll(array $collection, callable $method): bool
返回true
如果提供的函数返回true
对于集合的所有元素,false
。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [2, 3, 4, 5];
$result = $helper->validateAll(
$source,
function ($element) {
return $element > 1;
}
);
echo $result; // true
validateAny
¶
validateAny(array $collection, callable $method): bool
返回true
如果提供的函数返回true
对于集合中至少一个元素,false
。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [1, 2, 3, 4, 5];
$result = $helper->validateAny(
$collection,
function ($element) {
return $element < 2;
}
);
echo $result; // true
whitelist
¶
whitelist(array $collection, array $whiteList): array
通过白名单中的元素获得的键来包含数组的元素
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'value-1',
' key ' => 'value-2',
5 => 'value-3',
6 => 'value-4',
7 => 'value-5',
' key-2' => 'value-6',
'key-3 ' => 'value-7',
'key-4' => 'value-8',
];
$whiteList = [
7,
5,
0,
'key-3 ',
null,
-13,
new stdClass(),
[],
3.1415,
];
$result = $helper->whitelist($source, $blackList);
var_dump($result);
// [
// 0 => 'value-1',
// 5 => 'value-3',
// 7 => 'value-5',
// 'key-3 ' => 'value-7',
// ];
basename
¶
basename(string $uri, string $suffix = null): string
从给定路径获取文件名,功能等同于 PHP 的basename()
但它支持非 ASCII 编码。PHP 的basename()
不正确支持流或者以非 US-ASCII 字符开头的文件名。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = '/etc/sudoers.d';
$result = $helper->basename($source);
echo $result; // .d
$source = '/root/ελληνικά.txt';
$result = $helper->basename($source);
echo $result; // 'ελληνικά.txt'
decode
¶
decode(string $data, bool $associative = false, int $depth = 512, int $options = 0): string
使用json_decode
解码字符串,如果 JSON 数据无法解码,则抛出异常
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = '{"one":"two","0":"three"}';
$result = $helper->decode($source);
var_dump($result);
// [
// 'one' => 'two',
// 'three',
// ];
encode
¶
encode($data, int $options = 0, int $depth = 512): string
使用json_encode
编码字符串,如果 JSON 数据无法编码,则抛出异常
如果未指定以下选项则使用它们:json_encode
- JSON_HEX_TAG
- JSON_HEX_APOS
- JSON_HEX_AMP
- JSON_HEX_QUOT
- JSON_UNESCAPED_SLASHES
- JSON_THROW_ON_ERROR
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = [
'one' => 'two',
'three',
];
$result = $helper->encode($source);
echo $result; // '{"one":"two","0":"three"}'
isBetween
¶
isBetween(int $value, int $start, int $end): bool
检查数字是否在某个范围内
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$result = $helper->isBetween(5, 1, 10);
echo $result; // true
camelize
¶
camelize(string $text, string $delimiters = null, bool $lowerFirst = false): string
将字符串转换为UpperCamelCase或lowerCamelCase格式
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'came-li-ze';
$result = $helper->camelize($source);
echo $result; // CameLiZe
concat
¶
concat(string $delimiter, string $first, string $second, string ...$arguments): string
使用分隔符连接字符串,仅一次,不进行重复
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$result = $helper->concat(
'.',
'@test.',
'.test2.',
'.test',
'.34'
);
$result = $helper->concat($source);
echo $result; // '@test.test2.test.34'
countVowels
¶
countVowels(string $text): int
返回提供字符串中的元音数。使用正则表达式计算字符串中的元音(A, E, I, O, U)数量
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'Luke, I am your father!';
$result = $helper->countVowels($source);
echo $result; // 9
decapitalize
¶
decapitalize(string $text, bool $upperRest = false, string $encoding = 'UTF-8'): string
将首字母小写然后与字符串其余部分组合。省略upperRest参数以保持字符串其余部分不变,或设为true将其转为大写。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'BeetleJuice';
$result = $helper->decapitalize($source);
echo $result; // 'beetleJuice'
decrement
¶
decrement(string $text, string $separator = '_'): string
从字符串末尾移除一个数字,或者如果该数字已经存在则将其减一
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'file_2';
$result = $helper->decrement($source);
echo $result; // 'file_1'
dirFromFile
¶
dirFromFile(string $file): string
接受一个文件名(不带扩展名),并返回计算后的目录结构,文件名位于最后
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'abcdef12345.jpg';
$result = $helper->dirFromFile($source);
echo $result; // 'ab/cd/ef/12/3/'
dirSeparator
¶
dirSeparator(string $directory): string
接受目录名并确保其以DIRECTORY_SEPARATOR
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = '/home/phalcon//';
$result = $helper->dirSeparator($source);
echo $result; // '/home/phalcon/'
dynamic
¶
dynamic(string $text, string $leftDelimiter = "{", string $rightDelimiter = "}", string $separator = "|"): string
结尾
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = '{Hi|Hello}, my name is Bob!';
$result = $helper->dynamic($source);
echo $result; // 'Hi, my name is Bob!'
$result = $helper->dynamic($source);
echo $result; // 'Hello, my name is Bob!'
endsWith
¶
endsWith(string $haystack, string $needle, bool $ignoreCase = true): bool
返回true
如果字符串以给定字符串结尾的话,false
否则
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'abcdef';
$result = $helper->endsWith($source, 'ef');
echo $result; // true
firstBetween
¶
firstBetween(string $text, string $start, string $end): string
返回参数start和end所指定的两个字符串之间的第一个字符串。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'This is a [custom] string';
$result = $helper->firstBetween($source, '[', ']');
echo $result; // 'custom'
friendly
¶
friendly(string $text, string $separator = '-', bool $lowercase = true, mixed $replace = null): string
将文本转换为 URL 友好格式。将常见的带重音字符替换为对应的拉丁字符。如果传递了replace
字符串或数组,也将使用它将这些字符替换为空格。
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'This is a Test';
$result = $helper->friendly($source);
echo $result; // 'this-is-a-test'
humanize
¶
humanize(string $text): string
将带有下划线或短横线的文本转换为易读格式
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'kittens-are_cats';
$result = $helper->humanize($source);
echo $result; // 'kittens are cats'
includes
¶
includes(string $haystack, string $needle): bool
判断一个字符串是否包含另一个字符串
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'Phalcon Framework';
$result = $helper->includes($source, 'Framework');
echo $result; // true
increment
¶
increment(string $text, string $separator = '_'): string
在字符串末尾添加一个数字,或者如果该数字已经存在则将其加一
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'file_1';
$result = $helper->increment($source);
echo $result; // 'file_2'
interpolate
¶
interpolate(string $message, array $context = [], string $leftToken = "%", string $rightToken = "%"): string
将上下文值插入到消息占位符中。默认情况下,左右标记是%
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = '%date% (YYYY-MM-DD) %level% (0-9)';
$data = [
'date' => '2020-09-09',
'level' => 'CRITICAL',
];
$result = $helper->interpolate($source, $data);
echo $result; // '2020-09-09 (YYYY-MM-DD) CRITICAL (0-9)'
isAnagram
¶
isAnagram(string $first, string $second): bool
比较两个字符串并返回true
如果两个字符串是变位词,false
否则
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'rail safety';
$target = 'fairy tales';
$result = $helper->isAnagram($source, $target);
echo $result; // true
isLower
¶
isLower(string $text, string $encoding = 'UTF-8'): bool
返回true
如果给定字符串全部为小写,false
否则
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'phalcon framework';
$result = $helper->isLower($source);
echo $result; // true
isPalindrome
¶
isPalindrome(string $text): bool
返回true
如果给定字符串为回文,false
否则
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'racecar';
$result = $helper->isPalindrome($source);
echo $result; // true
isUpper
¶
isUpper(string $text, string $encoding = 'UTF-8'): bool
返回true
如果给定字符串全部为大写,false
否则
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'PHALCON FRAMEWORK';
$result = $helper->isUpper($source);
echo $result; // true
kebabCase
¶
kebabCase(string $text, string $delimiters = null): string
将字符串转换为 kebab-case 格式
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'customer_session';
$result = $helper->kebabCase($source);
echo $result; // 'customer-session'
len
¶
len(string $text, string $encoding = 'UTF-8'): int
使用以下方式计算字符串长度:mb_strlen
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'abcdef';
$result = $helper->len($source);
echo $result; // 6
lower
¶
lower(string $text, string $encoding = 'UTF-8'): string
使用mbstring
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'Phalcon Framework';
$result = $helper->lower($source);
echo $result; // 'phalcon framework'
pascalCase
¶
pascalCase(string $text, string $delimiters = null): string
将字符串转换为 PascalCase 格式
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'customer-session';
$result = $helper->pascalCase($source);
echo $result; // 'CustomerSession'
prefix
¶
prefix($text, string $prefix): string
使用提供的前缀对文本进行前缀添加
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'Framework';
$result = $helper->prefix($source, 'Phalcon');
echo $result; // 'PhalconFramework'
random
¶
random(int $type = 0, int $length = 8): string
根据给定类型生成随机字符串。类型可以是:
常量 | 描述 |
---|---|
RANDOM_ALNUM | 仅包含字母数字 [a-zA-Z0-9] |
RANDOM_ALPHA | 仅包含字母 [azAZ] |
RANDOM_DISTINCT | 仅包含不含类似字符的大写字母数字 [2345679ACDEFHJKLMNPRSTUVWXYZ] |
RANDOM_HEXDEC | 仅包含十六进制字符 [0-9a-f] |
RANDOM_NOZERO | 仅包含非零数字 [1-9] |
RANDOM_NUMERIC | 仅包含数字 [0-9] |
<?php
use Phalcon\Support\Helper\Str\Random;
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
echo $helper->random(Random::RANDOM_ALNUM); // 4
echo $helper->random(Random::RANDOM_ALNUM); // 2
echo $helper->random(Random::RANDOM_ALNUM); // 1
echo $helper->random(Random::RANDOM_ALNUM); // 3
reduceSlashes
¶
reduceSlashes(string $text): string
将字符串中的多个斜杠减少为单个斜杠
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'app/controllers//IndexController';
$result = $helper->reduceSlashes($source);
echo $result; // 'app/controllers/IndexController'
snakeCase
¶
snakeCase(string $text, string $delimiters = null): string
将字符串转换为 snake_case 格式
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'customer-session';
$result = $helper->snakeCase($source);
echo $result; // 'customer_session'
startsWith
¶
startsWith(string $haystack, string $needle, bool $ignoreCase = true): bool
返回true
如果字符串以给定字符串开头的话,false
否则
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'abcdef';
$result = $helper->startsWith($source, 'ab');
echo $result; // true
suffix
¶
suffix($text, string $suffix): string
使用提供的后缀对文本进行后缀添加
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'Phalcon';
$result = $helper->suffix($source, 'Framework');
echo $result; // 'PhalconFramework'
ucwords
¶
ucwords(string $text, string $encoding = 'UTF-8'): string
每个单词的首字母大写
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'phalcon framework';
$result = $helper->ucwords($source);
echo $result; // 'Phalcon Framework'
uncamelize
¶
uncamelize(string $text, string $delimiters = '_'): string
将字符串转换为非驼峰命名格式
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'CameLiZe';
$result = $helper->uncamelize($source);
echo $result; // came-li-ze
underscore
¶
underscore(string $text): string
将空格转换为下划线
<?php
use Phalcon\Support\HelperFactory;
$helper = new HelperFactory();
$source = 'Phalcon Framework';
$result = $helper->underscore($source);
echo $result; // 'Phalcon_Framework'
upper
¶
upper(string $text, string $encoding = 'UTF-8'): string
使用将字符串转换为大写mbstring