class CRM_Utils_String

This class contains string functions.

Constants

COMMA

SEMICOLON

SPACE

TAB

LINEFEED

CARRIAGELINE

LINECARRIAGE

CARRIAGERETURN

ALPHANUMERIC

List of all letters and numbers

Methods

static string
titleToVar(string $title, int $maxLength = 31)

Convert a display name into a potential variable name.

static string
munge(string $name, string $char = '_', int $len = 63)

Replace all non alpha numeric characters and spaces with the replacement character.

static string
convertStringToCamel(string $string)

Convert possibly underscore separated words to camel case with special handling for 'UF' e.g membership_payment returns MembershipPayment

static string
rename(string $name, int $len = 4)

Takes a variable name and munges it randomly into another variable name.

static string
getClassName(string $string, string $char = '_')

Takes a string and returns the last tuple of the string.

static 
append(string $str, string $delim, mixed $name)

Appends a name to a string and separated by delimiter.

static bool
isAscii(string $str, bool $utf8 = TRUE)

Determine if the string is composed only of ascii characters.

static array
regex(string $str, array $regexRules)

Determine the string replacements for redaction.

static mixed
redaction($str, $stringRules)

No description

static bool
isUtf8(string $str)

Determine if a string is composed only of utf8 characters

static bool
match(string $url1, string $url2)

Determine if two hrefs are equivalent (fuzzy match)

static string|null
extractURLVarValue(string $query)

Extract the civicrm path from the url.

static bool
strtobool(string $str)

Translate a true/false/yes/no string to a 0 or 1 value

static bool
strtoboolstr(string $str)

Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false

static string
htmlToText(string $html)

Convert a HTML string into a text one using html2text

static 
extractName($string, array $params)

No description

static array
makeArray($string)

No description

static string
stripAlternatives(string $full)

Given an ezComponents-parsed representation of a text with alternatives return only the first one

static string
stripSpaces(string $string)

Strip leading, trailing, double spaces from string used for postal/greeting/addressee

static string
stripPathChars(string $string, array $search = NULL, string $replace = NULL)

clean the URL 'path' variable that we use to construct CiviCRM urls by removing characters from the path variable

static string
purifyHTML(string $string)

Use HTMLPurifier to clean up a text string and remove any potential xss attacks. This is primarily used in public facing pages which accept html as the input string

static string
ellipsify(string $string, int $maxLen)

Truncate $string; if $string exceeds $maxLen, place ".

static string
createRandom($len, $alphabet)

Generate a random string.

static array
parsePrefix($delim, string $string, null $defaultPrefix = NULL)

Examples: "admin foo" => array(NULL,"admin foo") "cms:admin foo" => array("cms", "admin foo")

static string
maskEmail(string $email, string $maskChar = '*', int $percent = 50)

This function will mask part of the the user portion of an Email address (everything before the @)

static bool
compareStr(string $strOne, string $strTwo, bool $case)

This function compares two strings.

static string
unstupifyUrl(string $htmlUrl)

Many parts of the codebase have a convention of internally passing around HTML-encoded URLs. This effectively means that "&" is replaced by "&" (because most other odd characters are %-escaped in URLs; and %-escaped strings don't need any extra escaping in HTML).

static string
simplifyURL(string $url, bool $forceHttps = FALSE)

When a user supplies a URL (e.g. to an image), we'd like to: - Remove the protocol and domain name if the URL points to the current site.

static array
simpleParseUrl(string $url)

A simplified version of PHP's parse_url() function.

static string
htmlAttributes(array $attributes)

Formats a string of attributes for insertion in an html tag.

static bool
startsWith(string $string, string $fragment)

Determine if $string starts with $fragment.

static bool
endsWith(string $string, string $fragment)

Determine if $string ends with $fragment.

static array
filterByWildcards(string|array $patterns, array $allStrings, bool $allowNew = FALSE)

No description

static mixed
unserialize(string|NULL $string)

Safely unserialize a string of scalar or array values (but not objects!)

Details

at line 59
static string titleToVar(string $title, int $maxLength = 31)

Convert a display name into a potential variable name.

Parameters

string $title title of the string
int $maxLength

Return Value

string An equivalent variable name.

at line 84
static string munge(string $name, string $char = '_', int $len = 63)

Replace all non alpha numeric characters and spaces with the replacement character.

Parameters

string $name The name to be worked on.
string $char The character to use for non-valid chars.
int $len Length of valid variables.

Return Value

string returns the manipulated string

at line 112
static string convertStringToCamel(string $string)

Convert possibly underscore separated words to camel case with special handling for 'UF' e.g membership_payment returns MembershipPayment

Parameters

string $string

Return Value

string

at line 149
static string rename(string $name, int $len = 4)

Takes a variable name and munges it randomly into another variable name.

Parameters

string $name Initial Variable Name.
int $len Length of valid variables.

Return Value

string Randomized Variable Name

at line 167
static string getClassName(string $string, string $char = '_')

Takes a string and returns the last tuple of the string.

Useful while converting file names to class names etc

Parameters

string $string The input string.
string $char Character used to demarcate the components

Return Value

string The last component

at line 189
static append(string $str, string $delim, mixed $name)

Appends a name to a string and separated by delimiter.

Does the right thing for an empty string

Parameters

string $str The string to be appended to.
string $delim The delimiter to use.
mixed $name The string (or array of strings) to append.

at line 228
static bool isAscii(string $str, bool $utf8 = TRUE)

Determine if the string is composed only of ascii characters.

Parameters

string $str Input string.
bool $utf8 Attempt utf8 match on failure (default yes).

Return Value

bool true if string is ascii

at line 272
static array regex(string $str, array $regexRules)

Determine the string replacements for redaction.

on the basis of the regular expressions

Parameters

string $str Input string.
array $regexRules Regular expression to be matched w/ replacements.

Return Value

array array of strings w/ corresponding redacted outputs

at line 310
static mixed redaction($str, $stringRules)

Parameters

$str
$stringRules

Return Value

mixed

at line 330
static bool isUtf8(string $str)

Determine if a string is composed only of utf8 characters

Parameters

string $str Input string.

Return Value

bool

at line 359
static bool match(string $url1, string $url2)

Determine if two hrefs are equivalent (fuzzy match)

Parameters

string $url1 The first url to be matched.
string $url2 The second url to be matched against.

Return Value

bool true if the urls match, else false

at line 383
static string|null extractURLVarValue(string $query)

Extract the civicrm path from the url.

Parameters

string $query A url string.

Return Value

string|null civicrm url (eg: civicrm/contact/search)

at line 407
static bool strtobool(string $str)

Translate a true/false/yes/no string to a 0 or 1 value

Parameters

string $str The string to be translated.

Return Value

bool

at line 426
static bool strtoboolstr(string $str)

Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false

Parameters

string $str The string to be translated.

Return Value

bool

at line 451
static string htmlToText(string $html)

Convert a HTML string into a text one using html2text

Parameters

string $html The string to be converted.

Return Value

string the converted string

at line 464
static extractName($string, array $params)

Parameters

$string
array $params

at line 519
static array makeArray($string)

Parameters

$string

Return Value

array

at line 543
static string stripAlternatives(string $full)

Given an ezComponents-parsed representation of a text with alternatives return only the first one

Parameters

string $full All alternatives as a long string (or some other text).

Return Value

string only the first alternative found (or the text without alternatives)

at line 567
static string stripSpaces(string $string)

Strip leading, trailing, double spaces from string used for postal/greeting/addressee

Parameters

string $string Input string to be cleaned.

Return Value

string the cleaned string

at line 585
static string stripPathChars(string $string, array $search = NULL, string $replace = NULL)

clean the URL 'path' variable that we use to construct CiviCRM urls by removing characters from the path variable

Parameters

string $string The input string to be sanitized.
array $search The characters to be sanitized.
string $replace The character to replace it with.

Return Value

string the sanitized string

at line 642
static string purifyHTML(string $string)

Use HTMLPurifier to clean up a text string and remove any potential xss attacks. This is primarily used in public facing pages which accept html as the input string

Parameters

string $string The input string.

Return Value

string the cleaned up string

at line 666
static string ellipsify(string $string, int $maxLen)

Truncate $string; if $string exceeds $maxLen, place ".

.." at the end

Parameters

string $string
int $maxLen

Return Value

string

at line 680
static string createRandom($len, $alphabet)

Generate a random string.

Parameters

$len
$alphabet

Return Value

string

at line 702
static array parsePrefix($delim, string $string, null $defaultPrefix = NULL)

Examples: "admin foo" => array(NULL,"admin foo") "cms:admin foo" => array("cms", "admin foo")

Parameters

$delim
string $string E.g. "view all contacts". Syntax: "[prefix:]name".
null $defaultPrefix

Return Value

array (0 => string|NULL $prefix, 1 => string $value)

at line 725
static string maskEmail(string $email, string $maskChar = '*', int $percent = 50)

This function will mask part of the the user portion of an Email address (everything before the @)

Parameters

string $email The email address to be masked.
string $maskChar The character used for masking.
int $percent The percentage of the user portion to be masked.

Return Value

string returns the masked Email address

at line 751
static bool compareStr(string $strOne, string $strTwo, bool $case)

This function compares two strings.

Parameters

string $strOne String one.
string $strTwo String two.
bool $case Boolean indicating whether you want the comparison to be case sensitive or not.

Return Value

bool TRUE (string are identical); FALSE (strings are not identical)

at line 787
static string unstupifyUrl(string $htmlUrl)

Many parts of the codebase have a convention of internally passing around HTML-encoded URLs. This effectively means that "&" is replaced by "&" (because most other odd characters are %-escaped in URLs; and %-escaped strings don't need any extra escaping in HTML).

Parameters

string $htmlUrl URL with HTML entities.

Return Value

string URL without HTML entities

at line 819
static string simplifyURL(string $url, bool $forceHttps = FALSE)

When a user supplies a URL (e.g. to an image), we'd like to: - Remove the protocol and domain name if the URL points to the current site.

  • Keep the domain name for remote URLs.
    • Optionally, force remote URLs to use https instead of http (which is useful for images)

Parameters

string $url The URL to simplify. Examples: "https://example.org/sites/default/files/coffee-mug.jpg" "sites/default/files/coffee-mug.jpg" "http://i.stack.imgur.com/9jb2ial01b.png"
bool $forceHttps = FALSE If TRUE, ensure that remote URLs use https. If a URL with http is supplied, then we'll change it to https. This is useful for situations like showing a premium product on a contribution, because (as reported in CRM-14283) if the user gets a browser warning like "page contains insecure elements" on a contribution page, that's a very bad thing. Thus, even if changing http to https breaks the image, that's better than leaving http content in a contribution page.

Return Value

string The simplified URL. Examples: "/sites/default/files/coffee-mug.jpg" "https://i.stack.imgur.com/9jb2ial01b.png"

at line 853
static array simpleParseUrl(string $url)

A simplified version of PHP's parse_url() function.

Parameters

string $url e.g. "https://example.com:8000/foo/bar/?id=1#fragment"

Return Value

array Will always contain keys 'host+port' and 'path+query', even if they're empty strings. Example: [ 'host+port' => "example.com:8000", 'path+query' => "/foo/bar/?id=1", ]

at line 872
static string htmlAttributes(array $attributes)

Formats a string of attributes for insertion in an html tag.

Parameters

array $attributes

Return Value

string

at line 889
static bool startsWith(string $string, string $fragment)

Determine if $string starts with $fragment.

Parameters

string $string The long string.
string $fragment The fragment to look for.

Return Value

bool

at line 906
static bool endsWith(string $string, string $fragment)

Determine if $string ends with $fragment.

Parameters

string $string The long string.
string $fragment The fragment to look for.

Return Value

bool

at line 921
static array filterByWildcards(string|array $patterns, array $allStrings, bool $allowNew = FALSE)

Parameters

string|array $patterns
array $allStrings
bool $allowNew Whether to return new, unrecognized names.

Return Value

array

at line 959
static mixed unserialize(string|NULL $string)

Safely unserialize a string of scalar or array values (but not objects!)

Use xkerman/restricted-unserialize to unserialize strings using PHP's serialization format. restricted-unserialize works like PHP's built-in unserialize function except that it does not deserialize object instances, making it immune to PHP Object Injection {see https://www.owasp.org/index.php/PHP_Object_Injection} vulnerabilities.

Note: When dealing with user inputs, it is generally recommended to use safe, standard data interchange formats such as JSON rather than PHP's serialization format when dealing with user input.

Parameters

string|NULL $string

Return Value

mixed