Byt Wiki

String Functions

This file contains various utility functions for string manipulation.

nl2brCustom($text)

Convert newlines to HTML line breaks.

Parameters:

Returns:

Usage:

$htmlText = nl2brCustom("Line 1\nLine 2");
echo $htmlText; // Line 1<br>Line 2

escapeJs($str)

Escape single quotes for JavaScript.

Parameters:

Returns:

Usage:

$jsString = escapeJs("It's a test.");
echo $jsString; // It\'s a test.

slugify($str)

Generate a URL slug from a string.

Parameters:

Returns:

Usage:

$slug = slugify('Hello World!');
echo $slug; // hello-world

truncate($str, $length = 100, $append = '...')

Truncate a string to a specified length.

Parameters:

Returns:

Usage:

$shortText = truncate('This is a long text.', 10);
echo $shortText; // This is a...

startsWith($haystack, $needle)

Check if a string starts with a given substring.

Parameters:

Returns:

Usage:

$isStart = startsWith('Hello World', 'Hello');
echo $isStart; // 1 (true)

endsWith($haystack, $needle)

Check if a string ends with a given substring.

Parameters:

Returns:

Usage:

$isEnd = endsWith('Hello World', 'World');
echo $isEnd; // 1 (true)

toUpperCase($str)

Convert a string to uppercase.

Parameters:

Returns:

Usage:

$upperStr = toUpperCase('hello');
echo $upperStr; // HELLO

toLowerCase($str)

Convert a string to lowercase.

Parameters:

Returns:

Usage:

$lowerStr = toLowerCase('HELLO');
echo $lowerStr; // hello

capitalizeWords($str)

Capitalize the first letter of each word in a string.

Parameters:

Returns:

Usage:

$capitalizedStr = capitalizeWords('hello world');
echo $capitalizedStr; // Hello World

strReplace($search, $replace, $subject)

Replace a substring in a string.

Parameters:

Returns:

Usage:

$newStr = strReplace('world', 'PHP', 'Hello world');
echo $newStr; // Hello PHP

repeatChar($char, $times)

Generate a string of repeated characters.

Parameters:

Returns:

Usage:

$repeated = repeatChar('*', 10);
echo $repeated; // **********