Str



Str

Introduction

Str is useful for manipulating string data, you can instantiate it by calling the namespace.

use Str;

Basic Usage

ASCII

Transliterate a UTF-8 value to ASCII:

echo Str::ascii("café äëïöü");

Output:

cafe aeiou

Camel

Convert a value to camel case:

echo Str::camel("hello world!");

Output:

helloWorld!

Contains

Determine if a given string contains a given substring, will return a bool:

echo Str::contains("Hello World!", "World");

Output:

1

Starts With

Determine if a given string starts with a given substring, will return a bool:

echo Str::startsWith("Hello World!", "H");

Output:

1

Ends With

Determine if a given string ends with a given substring, will return a bool:

echo Str::endsWith("Hello World!", "!");

Output:

1

Finish

Cap a string with a single instance of a given value:

echo Str::finish("Hello World!", "111");

Output:

Hello World!111

Is

Cap a string with a single instance of a given value, will return a bool:

echo Str::is("Hello World!", "Hello World!");

Output:

1

Length

Return the length of the given string:

echo Str::length("Hello World!");

Output:

12

Limit

Limit the number of characters in a string:

echo Str::limit("Hello World!", 5, "[...]");

Output:

Hello[...]

Lower

Convert the given string to lower-case:

echo Str::lower("Hello World!");

Output:

hello world!

Upper

Convert the given string to upper-case:

echo Str::upper("Hello World!");

Output:

HELLO WORLD!

Title

Convert the given string to title case:

echo Str::title("hello world!");

Output:

Hello World!

Snake

Convert the given string to snake case:

echo Str::snake("Hello World!");

Output:

hello_world!

Studly

Convert the given string to studly case:

echo Str::studly("Hello World!");

Output:

HelloWorld!

ucfirst

Make a string's first character uppercase:

echo Str::ucfirst("hello world!");

Output:

Hello world!

Words

Limit the number of words in a string:

echo Str::words("Nova Framework is a simple but powerful MVC PHP Framework for building web applications.", 10, "[...]");

Output:

Nova Framework is a simple but powerful MVC PHP Framework

Plural

Get the plural form of an English word:

echo Str::plural("World");

Output:

Worlds

Singular

Get the singular form of an English word:

echo Str::singular("Worlds");

Output:

World

Random Bytes

Generate a more truly "random" bytes:

echo Str::randomBytes(16);

Output:

��3!��J�.wLZS*��

OpenSSL must be enabled.

Quick Random

Generate a "random" alpha-numeric string.

Should not be considered sufficient for cryptography, etc:

echo Str::quickRandom(16);

Output:

lVsrlIVt4hdUf8Nn

Equals

Compares two strings using a constant-time algorithm, will return a bool:

echo Str::equals("Nova Framework", "Nova Framework");

Output:

1

Slug

Generate a URL friendly "slug" from a given string:

echo Str::slug("Nova Framework", "-");

Output:

nova-framework

Substr

Returns the portion of string specified by the start and length parameters:

echo Str::substr("Nova Framework", 0, 4);

Output:

Nova