str

String helpers for casing, slugging, padding, wrapping, truncation and inspection.

v1.0.0Text & stringsbundledimport "str";
Overview Installation Documentation Examples

Overview

Rich, zero-dependency string manipulation helpers for HolyPHP — casing conversions, URL slugs, truncation, padding, word wrapping, prefix/suffix/substring inspection, and reversal.

bundled — works offline zero dependencies camel snake

Quick start

import "str";

println(str_camel("hello_world_example"));   // "helloWorldExample"
println(str_snake("XMLHttpRequest"));        // "xml_http_request"
println(str_kebab("Hello World!"));          // "hello-world"
println(str_pascal("user-profile-id"));      // "userProfileId" -> "UserProfileId"
println(str_slug("HolyPHP 1.0 Released!"));  // "holyphp-1-0-released"
println(str_truncate("Hello, world!", 8));   // "Hello..."
println(str_pad_left("42", 5, "0"));         // "00042"
println(str_wrap("the quick brown fox", 10));
println(str_reverse("HolyPHP"));             // "PHPyloH"

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install str

This adds the library to hphp.yaml, installs it into .hphp/packages/str/ and pins the exact version in hphp.lock:

require:
  str: "^1.0"

Use it

import "str";

str is bundled with the compiler, so import "str"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install str          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install str
Packagestr
Version1.0.0 (follows the compiler)
Entry filestr.hphp
Sourcepackages/lib/str ↗

Documentation

API Reference

Casing & Slugging

Truncation, Padding & Wrapping

Inspection & Transformation

Error Handling

All invalid arguments throw Exception with a clear str: ... message:

try {
    str_truncate("hello", -1);
} catch (Exception $e) {
    println($e->getMessage());
    // "str: str_truncate() maxLen must be >= 0, got -1"
}

try {
    str_pad_left("42", 5, "");
} catch (Exception $e) {
    println($e->getMessage());
    // "str: str_pad_left() pad string cannot be empty"
}

try {
    str_wrap("hello world", 0);
} catch (Exception $e) {
    println($e->getMessage());
    // "str: str_wrap() width must be > 0, got 0"
}

Examples

01Quick start

Bundled with the compiler — import "str"; works offline in any file, or add it to a project with hphp install str.

import "str";

println(str_camel("hello_world_example"));   // "helloWorldExample"
println(str_snake("XMLHttpRequest"));        // "xml_http_request"
println(str_kebab("Hello World!"));          // "hello-world"
println(str_pascal("user-profile-id"));      // "userProfileId" -> "UserProfileId"
println(str_slug("HolyPHP 1.0 Released!"));  // "holyphp-1-0-released"
println(str_truncate("Hello, world!", 8));   // "Hello..."
println(str_pad_left("42", 5, "0"));         // "00042"
println(str_wrap("the quick brown fox", 10));
println(str_reverse("HolyPHP"));             // "PHPyloH"
Try it
hphp install str
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_str.hphp.