IntStr
功能介紹
用于 整數(shù)
(需要轉(zhuǎn)換的整數(shù)必須在 0 ~ 9223372036854775668
范圍之內(nèi)) 和 字符串
的相互轉(zhuǎn)換,即:可以把一個(gè)字符串轉(zhuǎn)換成一個(gè)數(shù)字,反之,通過(guò)這個(gè)數(shù)字,我們可以得到之前的字符串。
可用于生成 url 短鏈接
。
相關(guān)class位置
- IntStr
-
namespace
:EasySwoole\Utility\IntStr
-
核心對(duì)象方法
toAlpha
生成基于 整數(shù)
對(duì)應(yīng)的 字符串
- int $number 要生成字符串的數(shù)字
public static function toAlpha(int $number): string
toNum
反向解析 字符串
對(duì)應(yīng)的 數(shù)字
- string $string 待解析的字符串
public static function toNum(string $string): int
基本使用
<?php
require __DIR__ . '/vendor/autoload.php';
// 傳入小于 9223372036854775668 的整數(shù),得到一個(gè)字符串,通過(guò)此字符串可以反向解析成對(duì)應(yīng)的數(shù)組
$str = \EasySwoole\Utility\IntStr::toAlpha(122407155078249761);
var_dump($str);
// 傳入字符串得到對(duì)應(yīng)的數(shù)字
$num = \EasySwoole\Utility\IntStr::toNum('EasySwoole');
var_dump($num);
// 用于生成短鏈接
$domain_prefix = 'https://easyswoole.com';
$path1 = \EasySwoole\Utility\IntStr::toNum('Preface');
$path2 = \EasySwoole\Utility\IntStr::toNum('intro');
$new_short_url = "{$domain_prefix}/{$path1}/{$path2}";
var_dump($new_short_url); // 生成的短鏈接
$real_path1 = \EasySwoole\Utility\IntStr::toAlpha($path1);
$real_path2 = \EasySwoole\Utility\IntStr::toAlpha($path2);
$real_url = "{$domain_prefix}/{$real_path1}/{$real_path2}";
var_dump($real_url); // 真實(shí)的請(qǐng)求地址
/**
* 輸出結(jié)果:
* string(10) "EasySwoole"
* int(122407155078249761)
* string(46) "https://easyswoole.com/1793938716421/272803253"
* string(36) "https://easyswoole.com/Preface/intro"
*/