String
MaplePHP Format String Library
This PHP library, named MaplePHP\DTO\Format\Str
, provides functionality for formatting strings. Below is a guide to the methods available in this library.
Formatting Methods
strVal(): string
Returns the value as a string.
excerpt(int $length = 40, string $ending = "..."): self
Excerpts/shortens the string to a specified length.
Parameters:
$length
: Total length of the excerpt.$ending
: String to add at the end of the excerpt.
nl2br(): self
Converts newlines to HTML
<br>
tags.
trailingSlash(): self
Ensures that the string always ends with a trailing slash.
stripTags(string $whitelist = ""): self
Strips HTML tags from the string.
Parameters:
$whitelist
: Allowed HTML tags.
specialchars(int $flag = ENT_QUOTES): self
Cleans GET/POST data for XSS protection.
Parameters:
$flag
: Flag forhtmlspecialchars
.
encode(int $flag = ENT_QUOTES): self
Alias for
specialchars
.
decode(?int $flag = ENT_QUOTES): self
Decodes HTML special characters.
Parameters:
$flag
: Flag forhtmlspecialchars_decode
.
clearBreaks(): self
Clears soft breaks from the string.
entityDecode(): self
Decodes HTML entities.
trim(): self
Trims whitespace from the string.
toLower(): self
Converts the string to lowercase.
toUpper(): self
Converts the string to uppercase.
ucfirst(): self
Converts the first character of the string to uppercase.
leadingZero(): self
Adds a leading zero to the string.
replaceSpaces(string $replaceWith = "-"): self
Replaces spaces in the string.
Parameters:
$replaceWith
: String to replace spaces with.
formatEmail(): self
Formats the string as an email address.
trimSpaces(): self
Trims and replaces multiple spaces between words with a single space.
formatSlug(): self
Formats the string as a slug.
replaceSpecialChar(): self
Replaces special characters with their counterparts.
urldecode(): self
URL-decodes the string.
urlencode(?array $find = null, ?array $replace = null): self
URL-encodes the string with optional string replacement.
Parameters:
$find
: Search values for replacement.$replace
: Replace values.
rawurldecode(): self
Raw URL-decodes the string.
rawurlencode(?array $find = null, ?array $replace = null): self
Raw URL-encodes the string with optional string replacement.
Parameters:
$find
: Search values for replacement.$replace
: Replace values.
replace($find, $replace): self
Replaces specified values in the string.
Parameters:
$find
: Search values for replacement.$replace
: Replace values.
toggleUrlencode(?array $find = null, ?array $replace = null): self
Decodes then encodes the URL with optional string replacement.
Parameters:
$find
: Search values for replacement.$replace
: Replace values.
Utility Methods
explode(string $delimiter): Arr
Explodes the string and returns an array instance.
Parameters:
$delimiter
: Delimiter for the explode operation.
camelCaseToArr(): Arr
Converts camelCase words to an array and returns an array instance.
extractPath(): self
Extracts the path from a URL.
dirname(): self
Gets only the dirname from the path.
trimTrailingSlash(): self
Trims the trailing slash from the string.
xxs(): self
Implements XXS protection.
toBool(): bool
Converts the string to a boolean value.
compare(string|int|float|bool|null $compare): bool
Compares the string to a given value.
Parameters:
$compare
: Value to compare.
toInt(): int
Converts the string to an integer.
toFloat(): float
Converts the string to a float.
Last updated