一个操作符包,希望能对那些在我们需要正则表达式时总是忘记它们的人有所帮助(manny是manipulation的简称)

v1.02 2022-12-17 18:11 UTC

README

hallindavid

Manny(简称操作符)

一个轻量级的PHP包,包含有用的通用操作符/格式化器。

使用Composer安装

composer require hallindavid/manny

如果使用Laravel,Manny别名应该可以自动发现并轻松使用,如下所示。

use Manny;

Manny::phone("8008008000"); // Returns: 800-800-8000

对于其他框架,你可能需要做

require_once "vendor/autoload.php"
use Manny;
Manny::phone("8008008000"); // Returns: 800-800-8000

Manny::phone

Manny::phone - 一个加拿大/美国电话格式化器 - 比之前重建得更好,来源于 hallindavid/phonehelper

定义

/**
* @param string $number
* @param array $options
*
*
* Default Options
*
*    $default_options = [
*        'showCountryCode'         => false,
*        'showAreaCode'            => true,
*        'showExchange'            => true,
*        'showLine'                => true,
*        'showExtension'           => false,
*        'prefix'                  => false,
*        'country_area_delimiter'  => false,
*        'area_exchange_delimiter' => '-',
*        'exchange_line_delimiter' => '-',
*        'line_extension_delimiter'=> ' ext. ',
*    ];
* @return string
*/
function phone($number, $options)

示例

Manny::phone("8008008000"); 
//outputs 800-800-8000

扩展Manny::phone

扩展电话类相当简单 - 这里是一个示例

class Brack10 extends Manny\Phone
{
    public function __construct($text)
    {
        parent::__construct($text);
        $this->showCountryCode = false;
        $this->showAreaCode = true;
        $this->showExchange = true;
        $this->showLine = true;
        $this->showExtension = false;
        $this->prefix = false;
        $this->country_area_delimiter = '(';
        $this->area_exchange_delimiter = ') ';
        $this->exchange_line_delimiter = '-';
        $this->line_extension_delimiter = ' ext. ';
    }
}

$phone = new Brack10("123456789123456");
$phone->format();
//Returns: (234) 567-8912

Manny::mask

一个用于格式化固定长度数据的掩码函数。(非常适合与 livewire/livewire 进行实时掩码)

定义

/**
 * @param string $target
 * @param string $pattern
 * @return string
 */
function mask($target, $pattern)

模式创建

A 应该是一个字母字符的占位符
1 应该是一个数字字符的占位符
所有其他字符都被视为格式化字符

示例

//US Social Security Number
Manny::mask("987654321", "111-11-1111"); //returns "987-65-4321"

//US Zip-code
Manny::mask("The whitehouse zip code is: 20500", "11111"); //returns "20500"

//Canada Postal Code
Manny::mask("K1M1M4", "A1A 1A1"); //

//outputs 987-65-4321

Manny::yoink

使用yoink从关联数组中提取特定的键值对,并且(可选地)传入默认值。

定义

/**
 * @param array $target - should be key-val associative array
 * @param array $elements - should be flat array with desired key names from target array
 * @param array $defaults (optional) - key-val associative array which will be appended to extracted key-value pairs before returning
 * @return array
 */
function yoink($target, $elements, $defaults = null)

示例

$array = ['id'  => '17', 'name'=> 'John Doe'];
$elements = ['name', 'role'];
$default_values = ['role'=> 'member'];
Manny::yoink($array, $elements, $default_values);  //Returns: ['name'=>'John Doe','role'=>'member'] ;

Manny::stripper

一个preg_replace的抽象,具有易于记忆的参数,以减少频繁的谷歌搜索

定义

/**
 * @param string     $text    - the subject of our stripping
 * @param array|null $options - an array with the return types you'd like
 * 
 *  keys can include the following types:
 *  alpha - keep the alphabetical characters (case-insensitive)
 * 	num - keep the digits (0-9)
 *  comma - keep commas
 *  colon - keep the : character
 *  dot - keep periods
 *  dash - keep dashes/hyphens
 *  space - keep spaces
 *  underscore - keep underscores
 *  pipe - keep pipe characters
 *  bracket - keep square brackets []
 *  parenthesis - keep parenthesis ()
 *  curly - keep curley braces (useful for handlebar syntax ex. {{ thing }} 
 * 
 * @return string
 */
function stripper($text, $options = null)

示例

$string = 'With only 5-10 hours of development, Dave built Manny, saving him atleast 10 seconds per day!';
$config = ['num', 'alpha', 'space'];
Manny::stripper($string,$config); 
//Returns: 'With only 510 hours of development Dave built Manny saving him atleast 10 seconds per day';

$alt_config = ['num'];
Manny::stripper($string,$alt_config); 
//Returns: '51010';

Manny::keep

Manny::stripper的别名,具有相同的功能(由于你实际上“保留”了在选项中定义的所有字符,这可以使代码的可读性更好)

示例

$string = 'I only want to "keep" the alpha, num, and spaces for this string!';
$options = ['alpha', 'num', 'space']
Manny::keep()
//Returns: 'I only want to keep the alpha num and spaces for this string'

Manny::crumble

一个preg_replace的抽象,具有易于记忆的参数,以减少频繁的谷歌搜索

定义

    /**
     * @param string $text - the subject of our crumbling
     * @param array $crumbs - an array of positive integers
     * @param bool $appendExtra - keys can include the following types
     * 
     * @return array
     */
    function crumble($string, $crumbs, $appendExtra = false)
    

示例

Manny::crumble("18008008000888", [1,3,3,4])
//Output: ["1","800","800","8000"];

//with append extra
Manny::crumble("18008008000888", [1,3,3,4],true)
//Output: ["1","800","800","8000", "888"];

Manny::percent

这是一个快速生成百分比的工具。它在处理之前清理坏数据,并具有一种有偏见的流程(例如,0/0 = 100%)

定义

    /**
     * @param int|float|string $num - the numerator
     * @param int|float|string $denom - the denominator
     * @param int $precision - keys can include the following types
     * 
     * @return float
     */
    function percent($num, $denom, $precision = 0)
    

示例

Manny::percent(1,8);
//Output: 12.5;

测试

该包格式有很多测试 - 要运行它们,先拉取包,然后

composer install
composer test

支持

想要表示感谢,您可以在社交媒体上分享该项目或

Buy Me A Coffee

问题

请通过GitHub问题跟踪器报告所有问题

贡献

给我发电子邮件,或者在我的推特上私信我,我很乐意允许其他贡献者。