# YsUtil

[YsUtil](https://github.com/exiang/ysUtil) is an utilities class for yii framework, also a collection of function snippets that make your web application building job easier. As YeeBase is using YsUtil, it is an integral part of it too.

Install thru composer: <https://packagist.org/packages/exiang/ys-util>

```
composer require exiang/ys-util
```

### Email Related

#### sendMail($receivers, $subject, $message, $params, $sendOnBehalf='')

Required `PHPMailer` composer package.

#### logSentMail($mailObj, $mailLogPath)

### String Manipulation

#### html2text($html)

Required `Html2Text` composer package. Change HTML to plain text string. For example, use in generate alternate text version of email content.

#### splitFullName2FirstLast($name)

Return `array('firstName'=>$firstName, 'lastName'=>$lastName)` by spliting any full name string you insert, e.g.: Allen Tan.

#### generateUUID()

Generate Universal Unique ID which is useful for `code` . Internally try to generate thru `com_create_guid` . If this PHP function not found, then it fallback to use `mt_rand`  method.

#### slugify($text)

Turn any text into URL friendly slug format thru series of operations like: trim, lowercase, replace non letter or digits by -, remove unwanted characters and transliterate utf-8.

#### generateRandomKey($max=5, $min=4, $mode='alphanumeric')

Generate a random key code at specific length and mode (alphabet only or number only, or both)

$mode:&#x20;

* alphanumeric
* alpha
* numeric

#### generateRandomPassword($max='8', $min='8', $lowerCase=true)

#### nl2space($string)

#### truncate($string='', $limit=100, $pad='...')

#### mbWordWrap($string, $width=75, $break="\n", $cut=false)

#### widerKeyword($keyword)

#### null2empty($string)

#### strPutCsv($input, $delimiter = ',', $enclosure = '"')

### Array Manipulation

#### yskrsort($array)

#### string2Array($str, $separator=',')

#### generateArrayRange($start, $stop, $mode='1d')

#### convertToKeyValueArray($array, $code='key', $title='title')

#### convert1dTo2dArray($array)

### Data Format Related

#### decimal2Bui($decimal)

Convert decimal to base unit integer

#### bui2Decimal($bui)

Convert base unit integer back to decimal

#### m2km($meter, $showUnit=false)

Convert meter to kilometer

#### formatByte($bytes, $unit = "MB", $decimals = 2)

#### ssNum2Alpha($n)

Convert spreadsheet's column number to alphabet. Example:

* 1 -> A
* 26 -> Z
* 27 -> AA

#### isMd5($string)

Return `true` if a string is MD5 encoded format, else `false`.

#### isSha1($string)

Return `true` if a string is SHA1 encoded format, else `false`.

#### isAlphaNumeric($input)

Return `true` if a string is in alpha numeric (a-z, A-Z, 0-9 only) format, else `false`.

#### isEmailAddress($input)

Return `true` if a string is a valid email, else `false`. Internally using PHP `filter_var` `FILTER_VALIDATE_EMAIL`

#### isJson($string)

Return `true` if a string is valid JSON format, else `false`. Internally it used `json_decode` to check.

### File System Related

#### getMimeType($filename)

#### listDir($dir, $recur = true)

#### fileGetBinary($file)

#### getFileName($filename)

#### getFileExtension($filename)

### Date Time Related

#### timezone2offset($name)

#### convertTimezone($timestamp, $toTimezone, $fromTimezone='GMT')

take a timestamp from timezone A (preset to GMT) and convert to timezone B

#### timeElapsed($timestamp, $precision=5)

Time elapsed to human readable format (e.g. 2 days 45 min ago).

#### getWeekInMonth($timestamp, $rollover='sunday')

#### calculateAge($timestamp = 0, $now = 0)

#### monthNumber2Name($number, $format='F')

#### dateDiff($interval, $datefrom, $dateto, $using\_timestamps = false)

$interval:&#x20;

* **yyyy** - Number of full years&#x20;
* **q** - Number of full quarters&#x20;
* **m** - Number of full months&#x20;
* **y** - Difference between day numbers (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)&#x20;
* **d** - Number of full days&#x20;
* **w** - Number of full weekdays&#x20;
* **ww** - Number of full weeks&#x20;
* **h** - Number of full hours&#x20;
* **n** - Number of full minutes&#x20;
* **s** - Number of full seconds (default)

### Network & Cloudflare Related

#### ipInRange($ip, $range)

#### getRequestIP()

#### isCloudflare()

#### cloudflareCheckIP($ip)

#### cloudflareRequestsCheck()

### Others

#### toChinaseNum($num)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://exiang.gitbook.io/yeebase/others/ysutil.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
