PHP code example of antharuu / phyx

1. Go to this page and download the library: Download antharuu/phyx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antharuu / phyx example snippets


use Phyx\Str;
use Phyx\Arr;
use Phyx\Json;
use Phyx\Url;
use Phyx\Path;
use Phyx\Enums\CaseSensitivity;

Str::contains('Hello World', 'world', CaseSensitivity::Insensitive); // true
Arr::getPath($payload, 'user.profile.name', 'Guest');                // 'Guest'
Json::tryDecodeArray($json);                                         // ?array
Url::withQueryValue($url, 'page', 2);                                 // rebuilt URL
Path::join('/var', 'www', 'app');                                     // '/var/www/app'

use Phyx\Str;
use Phyx\Enums\CaseSensitivity;

// Native PHP asks you to remember argument order and sentinel values.
$position = strpos($haystack, $needle); // int|false

if ($position !== false) {
    // ...
}

// Phyx keeps the target value first and returns the shape you expect.
$position = Str::indexOf($haystack, $needle); // ?int

if ($position !== null) {
    // ...
}

Str::contains('Invoice.pdf', 'invoice', CaseSensitivity::Insensitive); // true, explicit policy

use Phyx\Str;
use Phyx\Enums\CaseSensitivity;
use Phyx\Enums\Ordering;
use Phyx\Enums\Side;

// Search and extraction.
Str::contains('Hello World', 'world', CaseSensitivity::Insensitive); // true
Str::startsWith('composer.json', 'composer');                        // true
Str::indexOf('café', 'fé');                                          // 2
Str::before('https://phyx.dev/docs', '://');                         // 'https'
Str::after('[email protected]', '@');                                   // 'phyx.dev'
Str::afterLast('/var/www/index.php', '/');                           // 'index.php'

// Multibyte-safe casing and length.
Str::lower('ÉCOLE');                                                 // 'école'
Str::capitalize('élise');                                            // 'Élise'
Str::length('café');                                                 // 4

// Slicing, replacing, trimming, padding.
Str::slice('Hello World', 6);                                        // 'World'
Str::replace('Hello World', 'World', 'Phyx');                        // 'Hello Phyx'
Str::trim('--draft--', '-', Side::End);                              // '--draft'
Str::pad('42', 5, '0', Side::Start);                                 // '00042'

// Comparison is configurable without multiplying method names.
Str::compare('item2', 'item10', ordering: Ordering::Natural);         // -1
Str::compare('Apple', 'apple', CaseSensitivity::Insensitive);         // 0

// Encoding, hashes and safe HTML output.
Str::toHex('abc');                                                   // '616263'
Str::fromHex('616263');                                              // 'abc'
Str::md5('hello');                                                   // '5d41402abc4b2a76b9719d911017c592'
Str::escapeHtml('<b>Tom & Jerry</b>');                               // '&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;'

use Phyx\Arr;
use Phyx\Enums\Comparison;
use Phyx\Enums\SortDirection;

$user = [
    'id' => 42,
    'profile' => ['name' => 'Ada'],
    'roles' => ['admin', 'editor'],
];

Arr::get($user, 'id');                         // 42
Arr::getPath($user, 'profile.name');           // 'Ada'
Arr::getPath($user, 'profile.avatar', 'none'); // 'none'
Arr::hasPath($user, 'roles.0');                // true

$updated = Arr::setPath($user, 'profile.name', 'Grace');

$user['profile']['name'];    // 'Ada'
$updated['profile']['name']; // 'Grace'

Arr::contains(['1', 2, 3], 1, Comparison::Strict); // false
Arr::pluck($rows, 'email');                        // list of emails
Arr::groupBy($orders, fn ($order) => $order['status']);
Arr::sortBy($users, fn ($user) => $user['name'], SortDirection::Ascending);
Arr::dot(['user' => ['name' => 'Ada']]);           // ['user.name' => 'Ada']

use Phyx\Json;

$json = '{"user":{"name":"Ada","active":true}}';

Json::isValid($json);                     // true
Json::decodeArray($json);                 // ['user' => ['name' => 'Ada', 'active' => true]]
Json::get($json, 'user.name');            // 'Ada'
Json::has($json, 'user.active');          // true

Json::set($json, 'user.name', 'Grace');   // '{"user":{"name":"Grace","active":true}}'
Json::remove($json, 'user.active');       // '{"user":{"name":"Ada"}}'

Json::tryDecodeArray('{broken json');     // null
Json::pretty(['name' => 'Ada']);          // formatted JSON string

use Phyx\Url;
use Phyx\Enums\QueryFormat;

$url = 'https://example.com/docs?lang=en#intro';

Url::scheme($url);                                  // 'https'
Url::host($url);                                    // 'example.com'
Url::path($url);                                    // '/docs'
Url::queryParameters($url);                         // ['lang' => 'en']
Url::isHttps($url);                                 // true

Url::withPath($url, '/guides');                     // 'https://example.com/guides?lang=en#intro'
Url::withQueryValue($url, 'page', 2);               // 'https://example.com/docs?lang=en&page=2#intro'
Url::withoutFragment($url);                         // 'https://example.com/docs?lang=en'
Url::encodeComponent('a value/with slash');         // 'a%20value%2Fwith%20slash'
Url::buildQuery(['q' => 'php helpers'], QueryFormat::Rfc3986);

use Phyx\Path;
use Phyx\Enums\PathStyle;

Path::join('/var', 'www', 'app');          // '/var/www/app'
Path::normalize('/var/www/../log');        // '/var/log'
Path::extension('/tmp/archive.tar.gz');    // 'gz'
Path::filename('/tmp/archive.tar.gz');     // 'archive.tar'
Path::segments('/var/www/app');            // ['var', 'www', 'app']

Path::toWindows('/var/www/app');           // '\\var\\www\\app'
Path::normalize('C:\\temp\\..\\app', PathStyle::Windows);

Path::exists('/var/www/app');              // explicit filesystem check
Path::real('/var/www/../log');             // ?string

use Phyx\Html;
use Phyx\Num;
use Phyx\Bytes;

Html::escape('<a href="/">Home</a>');        // '&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;'
Html::attribute('disabled', true);            // 'disabled'
Html::tag('strong', 'Warning');               // '<strong>Warning</strong>'

Num::average([10, 20, 30]);                   // 20.0
Num::clamp(120, 0, 100);                      // 100
Num::percentage(25, 200);                     // 12.5
Num::ordinal(3);                              // '3rd'

Bytes::length("\x00\x01\x02");              // 3
Bytes::toHex('abc');                          // '616263'
Bytes::fromHex('616263');                     // 'abc'
Bytes::toBase64Url('hello');                  // 'aGVsbG8'
Bytes::fromInts([2, 1]);                      // "\x02\x01"