PHP has plenty of built-in string functions. Table 3-1 lists a selection of them.
Table 3-1. The String Functions
Function | Purpose |
---|
chr | Returns a specific character, given its ASCII code |
chunk_split | Splits a string into smaller chunks |
crypt | Supports one-way string encryption (hashing) |
echo | Displays one or more strings |
explode | Splits a string on a substring |
html_entity_decode | Converts all HTML entities to their applicable characters |
htmlentities | Converts all applicable characters to HTML entities |
htmlspecialchars | Converts special characters to HTML entities |
implode | Joins array elements with a string |
ltrim | Strips whitespace from the beginning of a string |
number_format | Formats a number with grouped thousand separators |
ord | Returns the ASCII value of character |
parse_str | Parses the string into variables |
print | Displays a string |
printf | Displays a formatted string |
rtrim | Strips whitespace from the end of a string |
setlocale | Sets locale information |
similar_text | Calculates the similarity between two strings |
sprintf | Returns a formatted string |
sscanf | Parses input from a string according to a format |
str_ireplace | Case-insensitive version of the str_replace function. |
str_pad | Pads a string with another string |
str_repeat | Repeats a string |
str_replace | Replaces all occurrences of the search string with the replacement string |
str_shuffle | Shuffles a string randomly |
str_split | Converts a string to an array |
str_word_count | Returns information about words used in a string |
strcasecmp | Binary case-insensitive string comparison |
strchr | Alias of the strstr function |
strcmp | Binary-safe string comparison |
strip_tags | Strips HTML and PHP tags from a string |
stripos | Finds position of first occurrence of a case-insensitive string |
stristr | Case-insensitive version of the strstr function |
strlen | Gets a string's length |
strnatcasecmp | Case-insensitive string comparisons |
strnatcmp | String comparisons using a "natural order" algorithm |
strncasecmp | Binary case-insensitive string comparison of the first n characters |
strncmp | Binary-safe string comparison of the first n characters |
strpos | Finds position of first occurrence of a string |
strrchr | Finds the last occurrence of a character in a string |
strrev | Reverses a string |
strripos | Finds the position of last occurrence of a case-insensitive string |
strrpos | Finds the position of last occurrence of a char in a string |
strspn | Finds the length of initial segment matching mask |
strstr | Finds the first occurrence of a string |
strtolower | Converts a string to lowercase |
strtoupper | Converts a string to uppercase |
strtr | Translates certain characters |
substr_compare | Binary-safe (optionally case-insensitive) comparison of two strings from an offset |
substr_count | Counts the number of substring occurrences |
substr_replace | Replaces text within part of a string |
substr | Returns part of a string |
trim | Strips whitespace from the beginning and end of a string |