String Helper

The String Helper provides a collection of functions to manipulate, format, and sanitize strings in LavaLust applications. These helpers cover tasks such as limiting words, removing quotes, generating random strings, and performing string replacements.

Function Summary

Function

Description

str_insert()

Replaces placeholders in a string using an associative array of key-value pairs.

str_between()

Extracts substrings located between two delimiters.

str_after()

Returns the part of a string after a given search string.

str_before()

Returns the part of a string before a given search string.

str_limitwords()

Limits a string to a certain number of words.

str_limitchars()

Limits a string to a certain number of characters.

strip_slashes()

Removes slashes from a string or array recursively.

strip_quotes()

Removes single and double quotes from a string.

quotes_to_entities()

Converts quotes to HTML entities.

reduce_double_slashes()

Converts double slashes to single slashes except in URLs.

reduce_multiples()

Reduces multiple instances of a character to a single instance.

random_string()

Generates a random string of a specified type and length.

increment_string()

Appends or increments a number at the end of a string.

alternator()

Alternates between multiple strings on successive calls.

repeater()

Repeats a string a given number of times.

Available Functions

str_insert(array $keyValue, string $string)

Replaces placeholders in a string using an associative array.

Parameters:
  • $keyValue (array) – Key-value pairs to replace in the string.

  • $string (string) – The string containing placeholders.

Returns:

string — The modified string.

Example:

<?php
$text = str_insert(['{name}' => 'John'], 'Hello {name}!');
echo $text; // Output: Hello John!
str_between(string $left, string $right, string $string)

Returns all substrings between two delimiters.

Parameters:
  • $left (string) – Left delimiter.

  • $right (string) – Right delimiter.

  • $string (string) – The string to search in.

Returns:

array — Array of matched substrings.

str_after(string $search, string $string)

Returns the part of a string after a given search string.

Parameters:
  • $search (string) – Search string.

  • $string (string) – Input string.

Returns:

string — Portion after the search string.

str_before(string $search, string $string)

Returns the part of a string before a given search string.

Parameters:
  • $search (string) – Search string.

  • $string (string) – Input string.

Returns:

string — Portion before the search string.

str_limitwords(string $string, int $limit = 10, string $end = '...')

Limits a string to a specified number of words.

Parameters:
  • $string (string) – Input string.

  • $limit (int) – Maximum number of words (default 10).

  • $end (string) – Suffix to append if truncated (default ‘…’).

Returns:

string — Truncated string.

str_limitchars(string $string, int $limit = 100, string $end = '...')

Limits a string to a specified number of characters.

Parameters:
  • $string (string) – Input string.

  • $limit (int) – Maximum number of characters (default 100).

  • $end (string) – Suffix to append if truncated (default ‘…’).

Returns:

string — Truncated string.

strip_slashes(mixed $str)

Removes slashes from a string or recursively from an array.

Parameters:
  • $str (mixed) – String or array.

Returns:

mixed — Cleaned string or array.

strip_quotes(string $str)

Removes single and double quotes from a string.

Parameters:
  • $str (string) – Input string.

Returns:

string — String without quotes.

quotes_to_entities(string $str)

Converts single and double quotes to HTML entities.

Parameters:
  • $str (string) – Input string.

Returns:

string — String with quotes converted to entities.

reduce_double_slashes(string $str)

Converts double slashes to single slashes except in URLs.

Parameters:
  • $str (string) – Input string.

Returns:

string — String with reduced slashes.

reduce_multiples(string $str, string $character = ', ', bool $trim = FALSE)

Reduces multiple instances of a character to a single instance.

Parameters:
  • $str (string) – Input string.

  • $character (string) – Character to reduce (default ‘,’).

  • $trim (bool) – Trim the character from start/end (default FALSE).

Returns:

string — String with reduced multiples.

random_string(string $type = 'alnum', int $len = 8)

Generates a random string.

Parameters:
  • $type (string) – Type of string: basic, alpha, alnum, numeric, nozero, md5, sha1.

  • $len (int) – Number of characters (default 8).

Returns:

string — Random string.

increment_string(string $str, string $separator = '_', int $first = 1)

Appends or increments a number at the end of a string.

Parameters:
  • $str (string) – Input string.

  • $separator (string) – Separator before the number (default ‘_’).

  • $first (int) – Number to start incrementing from (default 1).

Returns:

string — Incremented string.

alternator()

Alternates between multiple strings on successive calls.

Returns:

string — Alternated string.

repeater(string $data, int $num = 1)

Repeats a string a specified number of times.

Parameters:
  • $data (string) – String to repeat.

  • $num (int) – Number of repeats (default 1).

Returns:

string — Repeated string.