CoolCMS API

StringHelper
in package

Static string helpers: conversion between name formats, slugs, truncation and prefix checks.

Table of Contents

Methods

camelToKebab()  : string
Converts camelCase to kebab-case.
camelToSnake()  : string
Converts camelCase to snake_case.
endsWith()  : bool
Checks whether a string ends with the given suffix.
kebabToCamel()  : string
Converts kebab-case to camelCase.
removeAccents()  : string
Removes diacritics from a string.
replaceWords()  : string
snakeToCamel()  : string
Converts snake_case to camelCase.
startsWith()  : bool
Checks whether a string starts with the given prefix.
toPascalCase()  : string
Converts a string to PascalCase.
toSlug()  : string
Converts a string to a URL-friendly slug.
truncate()  : string
Truncates a string to the given length and appends a suffix (an ellipsis by default).

Methods

camelToKebab()

Converts camelCase to kebab-case.

public static camelToKebab(string $input) : string
Parameters
$input : string

input string in camelCase

Return values
string —

output string in kebab-case

Examples:

  • getAll -> get-all
  • getUserData -> get-user-data
  • XMLHttpRequest -> xmlhttp-request (an uppercase run is not split)
  • iPhone -> i-phone

camelToSnake()

Converts camelCase to snake_case.

public static camelToSnake(string $input) : string
Parameters
$input : string

input string in camelCase

Return values
string —

output string in snake_case

Examples:

  • getAll -> get_all
  • getUserData -> get_user_data

endsWith()

Checks whether a string ends with the given suffix.

public static endsWith(string $haystack, string $needle[, bool $caseSensitive = true ]) : bool
Parameters
$haystack : string

string to search in

$needle : string

suffix to look for

$caseSensitive : bool = true

whether the comparison is case-sensitive

Return values
bool —

true when the string ends with the suffix

kebabToCamel()

Converts kebab-case to camelCase.

public static kebabToCamel(string $input[, bool $upperFirst = false ]) : string
Parameters
$input : string

input string in kebab-case

$upperFirst : bool = false

whether the first letter is upper-cased (PascalCase)

Return values
string —

output string in camelCase

Examples:

  • get-all -> getAll
  • get-user-data -> getUserData
  • xml-http-request -> xmlHttpRequest

removeAccents()

Removes diacritics from a string.

public static removeAccents(string $input) : string
Parameters
$input : string

input string with diacritics

Return values
string —

string without diacritics

replaceWords()

public static replaceWords(string $string, array<string|int, string> $blacklistedWordList[, string|array<string|int, string> $replaceBy = '...' ]) : string
Parameters
$string : string
$blacklistedWordList : array<string|int, string>
$replaceBy : string|array<string|int, string> = '...'
Return values
string

snakeToCamel()

Converts snake_case to camelCase.

public static snakeToCamel(string $input[, bool $upperFirst = false ]) : string
Parameters
$input : string

input string in snake_case

$upperFirst : bool = false

whether the first letter is upper-cased (PascalCase)

Return values
string —

output string in camelCase

Examples:

  • get_all -> getAll
  • get_user_data -> getUserData

startsWith()

Checks whether a string starts with the given prefix.

public static startsWith(string $haystack, string $needle[, bool $caseSensitive = true ]) : bool
Parameters
$haystack : string

string to search in

$needle : string

prefix to look for

$caseSensitive : bool = true

whether the comparison is case-sensitive

Return values
bool —

true when the string starts with the prefix

toPascalCase()

Converts a string to PascalCase.

public static toPascalCase(string $input) : string
Parameters
$input : string

input string

Return values
string —

output string in PascalCase

Examples:

  • get-all -> GetAll
  • get_user_data -> GetUserData
  • hello world -> HelloWorld

toSlug()

Converts a string to a URL-friendly slug.

public static toSlug(string $input[, string $separator = '-' ]) : string
Parameters
$input : string

input string

$separator : string = '-'

separator, a hyphen by default

Return values
string —

URL-friendly slug

Examples:

  • "Hello World!" -> "hello-world"
  • "Ångström" -> "angstrom"

truncate()

Truncates a string to the given length and appends a suffix (an ellipsis by default).

public static truncate(string $input, int $length[, string $suffix = '...' ][, bool $preserveWords = true ]) : string
Parameters
$input : string

input string

$length : int

maximum length

$suffix : string = '...'

suffix appended to a truncated string

$preserveWords : bool = true

whether to cut at a word boundary

Return values
string —

truncated string

On this page

Search results