PHP/string: Difference between revisions
from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
No edit summary |
No edit summary |
||
| Line 5: | Line 5: | ||
! || || colspan=5 | categories | ! || || colspan=5 | categories | ||
|- | |- | ||
! fx() name || description || escaping || | ! fx() name || description || escaping || return format || encryption || formatting || output | ||
{{!-!}} <code>addcslashes()</code> || Quote string with slashes in a C style || Y || string | |||
{{!-!}} <code>addslashes()</code> || Quote string with slashes || Y || string | |||
{{!-!}} ''<code>chop()</code>'' || Alias of rtrim | |||
{{!-!}} <code>count_chars()</code> || Return information about characters used in a string || || int | |||
{{!-!}} <code>crc32()</code> || Calculates the crc32 polynomial of a string || || int || Y | |||
{{!-!}} <code>crypt()</code> || one-way string hashing || || || Y | |||
{{!-!}} [https://www.php.net/manual/en/function.escapeshellarg.php <code>escapeshellarg()</code>] || Escape a string to be used as a shell argument || Y. | |||
{{!-!}} [https://www.php.net/manual/en/function.escapeshellcmd.php <code>escapeshellcmd()</code>] || Escape any execution-invoking characters. || Y | |||
{{!-!}} <code>fprintf()</code> || Write a formatted string to a stream || || || Y || || stream | |||
{{!-!}} <code>localeconv()</code> || Get numeric formatting information | |||
{{!-!}} <code>ltrim()</code> || Strip whitespace (or other characters) from the beginning of a string | |||
{{!-!}} <code>md5_file()</code> || Calculates the md5 hash of a given file | |||
|- | {{!-!}} <code>md5()</code> || Calculate the md5 hash of a string | ||
{{!-!}} <code>money_format()</code> || Formats a number as a currency string | |||
{{!-!}} <code>nl2br()</code> || Inserts HTML line breaks before all newlines in a string | |||
{{!-!}} <code>number_format()</code> || Format a number with grouped thousands | |||
{{!-!}} <code>ord()</code> || Convert the first byte of a string to a value between 0 and 255 | |||
{{!-!}} <code>parse_str()</code> || Parses the string into variables | |||
{{!-!}} <code>print()</code> || Output a string | |||
{{!-!}} <code>printf()</code>|| Output a formatted string | |||
{{!-!}} <code>quoted_printable_decode()</code> || Convert a quoted-printable string to an 8 bit string | |||
{{!-!}} <code>quoted_printable_encode()</code> || Convert a 8 bit string to a quoted-printable string | |||
{{!-!}} <code>quotemeta()</code> || Quote meta characters | |||
{{!-!}} <code>rtrim()</code> || Strip whitespace (or other characters) from the end of a string | |||
{{!-!}} <code>setlocale()</code> || Set locale information | |||
{{!-!}} <code>sha1_file()</code> || Calculate the sha1 hash of a file | |||
{{!-!}} <code>sha1()</code> || Calculate the sha1 hash of a string | |||
{{!-!}} <code>similar_text()</code> || Calculate the similarity between two strings | |||
{{!-!}} <code>soundex()</code> || Calculate the soundex key of a string | |||
{{!-!}} <code>sprintf()</code> || Return a formatted string | |||
{{!-!}} <code>sscanf()</code> || Parses input from a string according to a format | |||
{{!-!}} <code>str_contains()</code> || Determine if a string contains a given substring | |||
{{!-!}} <code>str_ends_with()</code> || Checks if a string ends with a given substring | |||
{{!-!}} <code>str_getcsv()</code> || Parse a CSV string into an array | |||
{{!-!}} <code>str_ireplace()</code> || Case-insensitive version of str_replace | |||
{{!-!}} <code>str_pad()</code> || Pad a string to a certain length with another string | |||
{{!-!}} <code>str_repeat()</code> || Repeat a string | |||
{{!-!}} <code>str_replace()</code> || Replace all occurrences of the search string with the replacement string. | |||
{{!-!}} <code>str_rot13()</code> || Perform the rot13 transform on a string. | |||
{{!-!}} <code>str_shuffle()</code> || Randomly shuffle a string. | |||
{{!-!}} <code>str_split()</code> || Convert a string to an array. | |||
{{!-!}} <code>str_starts_with()</code> || Checks if a string starts with a given substring | |||
{{!-!}} <code>str_word_count()</code> || Return information about words used in a string. | |||
{{!-!}} <code>strcasecmp()</code> || Binary safe case-insensitive string comparison | |||
{{!-!}} ''<code>strchr()</code>'' || alias of strstr | |||
{{!-!}} <code>strcmp()</code> || binary-safe string comparison | |||
{{!-!}} <code>strcoll()</code> || locale-based string comparison | |||
{{!-!}} <code>strcspn()</code> || Find length of initial segment that does ''not'' match a mask (opposite of <code>strspn()</code>). | |||
{{!-!}} <code>strip_tags()</code> || Strip HTML and PHP tags from a string. | |||
{{!-!}} <code>stripcslashes()</code> || Un-quote string quoted with <code>addcslashes()</code> | |||
{{!-!}} <code>stripos()</code> || Find the position of the first occurrence of a case-insensitive substring in a string. | |||
{{!-!}} [https://www.php.net/manual/en/function.stripslashes.php <code>stripslashes()</code>] || Un-quotes a quoted string | |||
{{!-!}} <code>stristr()</code> || Case-insensitive <code>strstr()</code> | |||
{{!-!}} <code>strlen()</code> || Get string length. || || int | |||
{{!-!}} <code>strnatcasecmp()</code> || case-insensitive string comparisons using a "natural order" algorithm | |||
{{!-!}} <code>strnatcmp()</code> || string comparisons using a "natural order" algorithm | |||
{{!-!}} <code>strncasecmp()</code> || binary-safe case-insensitive string comparison of the first n characters | |||
{{!-!}} <code>strncmp()</code> || binary-safe string comparison of the first n characters | |||
{{!-!}} <code>strpbrk()</code> || Search a string for any of a set of characters. | |||
{{!-!}} <code>strpos()</code> || Find the position of the first occurrence of a substring in a string. | |||
{{!-!}} <code>strrchr()</code> || Find the last occurrence of a character in a string. | |||
{{!-!}} <code>strrev()</code> || Reverse a string. | |||
{{!-!}} <code>strripos()</code> || Find the position of the last occurrence of a case-insensitive substring in a string. | |||
{{!-!}} <code>strrpos()</code> || Find the position of the last occurrence of a substring in a string. | |||
{{!-!}} <code>strspn()</code> || Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask (opposite of <code>strcspn()</code>). | |||
{{!-!}} <code>strstr()</code> || Find the first occurrence of a string in another string. | |||
{{!-!}} <code>strtok()</code> || Tokenize string | |||
{{!-!}} <code>strtolower()</code> || Make a string lowercase. | |||
{{!-!}} <code>strtoupper()</code> || Make a string uppercase. | |||
{{!-!}} <code>strtr()</code> || Translate characters or replace substrings | |||
{{!-!}} <code>substr_compare()</code> || Binary safe comparison of two strings from an offset, up to length characters | |||
{{!-!}} <code>substr_count()</code> || Count the number of substring occurrences | |||
{{!-!}} <code>substr_replace()</code> || Replace text within a portion of a string | |||
{{!-!}} <code>substr()</code> || Return part of a string | |||
{{!-!}} <code>trim()</code> || Strip whitespace (or other characters) from the beginning and end of a string | |||
{{!-!}} <code>ucfirst()</code> || Make a string's first character uppercase | |||
{{!-!}} <code>ucwords()</code> || Uppercase the first character of each word in a string | |||
{{!-!}} <code>utf8_decode()</code> || Converts a string from UTF-8 to ISO-8859-1, replacing invalid or unrepresentable characters | |||
{{!-!}} <code>utf8_encode()</code> || Converts a string from ISO-8859-1 to UTF-8 | |||
{{!-!}} <code>vfprintf()</code> || Write a formatted string to a stream. | |||
{{!-!}} <code>vprintf()</code> || Output a formatted string | |||
{{!-!}} <code>vsprintf()</code> || Return a formatted string | |||
{{!-!}} <code>wordwrap()</code> || Wrap a string to a given number of characters. | |||
|} | |} | ||
==Splitting/Joining== | ==Splitting/Joining== | ||
Revision as of 18:54, 5 January 2026
The official PHP docs don't really organize the string-functions listing very well.
Main Functions
| categories | ||||||
|---|---|---|---|---|---|---|
| fx() name | description | escaping | return format | encryption | formatting | output |
addcslashes() |
Quote string with slashes in a C style | Y | string | |||
addslashes() |
Quote string with slashes | Y | string | |||
chop() |
Alias of rtrim | |||||
count_chars() |
Return information about characters used in a string | int | ||||
crc32() |
Calculates the crc32 polynomial of a string | int | Y | |||
crypt() |
one-way string hashing | Y | ||||
escapeshellarg() |
Escape a string to be used as a shell argument | Y. | ||||
escapeshellcmd() |
Escape any execution-invoking characters. | Y | ||||
fprintf() |
Write a formatted string to a stream | Y | stream | |||
localeconv() |
Get numeric formatting information | |||||
ltrim() |
Strip whitespace (or other characters) from the beginning of a string | |||||
md5_file() |
Calculates the md5 hash of a given file | |||||
md5() |
Calculate the md5 hash of a string | |||||
money_format() |
Formats a number as a currency string | |||||
nl2br() |
Inserts HTML line breaks before all newlines in a string | |||||
number_format() |
Format a number with grouped thousands | |||||
ord() |
Convert the first byte of a string to a value between 0 and 255 | |||||
parse_str() |
Parses the string into variables | |||||
print() |
Output a string | |||||
printf() |
Output a formatted string | |||||
quoted_printable_decode() |
Convert a quoted-printable string to an 8 bit string | |||||
quoted_printable_encode() |
Convert a 8 bit string to a quoted-printable string | |||||
quotemeta() |
Quote meta characters | |||||
rtrim() |
Strip whitespace (or other characters) from the end of a string | |||||
setlocale() |
Set locale information | |||||
sha1_file() |
Calculate the sha1 hash of a file | |||||
sha1() |
Calculate the sha1 hash of a string | |||||
similar_text() |
Calculate the similarity between two strings | |||||
soundex() |
Calculate the soundex key of a string | |||||
sprintf() |
Return a formatted string | |||||
sscanf() |
Parses input from a string according to a format | |||||
str_contains() |
Determine if a string contains a given substring | |||||
str_ends_with() |
Checks if a string ends with a given substring | |||||
str_getcsv() |
Parse a CSV string into an array | |||||
str_ireplace() |
Case-insensitive version of str_replace | |||||
str_pad() |
Pad a string to a certain length with another string | |||||
str_repeat() |
Repeat a string | |||||
str_replace() |
Replace all occurrences of the search string with the replacement string. | |||||
str_rot13() |
Perform the rot13 transform on a string. | |||||
str_shuffle() |
Randomly shuffle a string. | |||||
str_split() |
Convert a string to an array. | |||||
str_starts_with() |
Checks if a string starts with a given substring | |||||
str_word_count() |
Return information about words used in a string. | |||||
strcasecmp() |
Binary safe case-insensitive string comparison | |||||
strchr() |
alias of strstr | |||||
strcmp() |
binary-safe string comparison | |||||
strcoll() |
locale-based string comparison | |||||
strcspn() |
Find length of initial segment that does not match a mask (opposite of strspn()).
| |||||
strip_tags() |
Strip HTML and PHP tags from a string. | |||||
stripcslashes() |
Un-quote string quoted with addcslashes()
| |||||
stripos() |
Find the position of the first occurrence of a case-insensitive substring in a string. | |||||
stripslashes() |
Un-quotes a quoted string | |||||
stristr() |
Case-insensitive strstr()
| |||||
strlen() |
Get string length. | int | ||||
strnatcasecmp() |
case-insensitive string comparisons using a "natural order" algorithm | |||||
strnatcmp() |
string comparisons using a "natural order" algorithm | |||||
strncasecmp() |
binary-safe case-insensitive string comparison of the first n characters | |||||
strncmp() |
binary-safe string comparison of the first n characters | |||||
strpbrk() |
Search a string for any of a set of characters. | |||||
strpos() |
Find the position of the first occurrence of a substring in a string. | |||||
strrchr() |
Find the last occurrence of a character in a string. | |||||
strrev() |
Reverse a string. | |||||
strripos() |
Find the position of the last occurrence of a case-insensitive substring in a string. | |||||
strrpos() |
Find the position of the last occurrence of a substring in a string. | |||||
strspn() |
Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask (opposite of strcspn()).
| |||||
strstr() |
Find the first occurrence of a string in another string. | |||||
strtok() |
Tokenize string | |||||
strtolower() |
Make a string lowercase. | |||||
strtoupper() |
Make a string uppercase. | |||||
strtr() |
Translate characters or replace substrings | |||||
substr_compare() |
Binary safe comparison of two strings from an offset, up to length characters | |||||
substr_count() |
Count the number of substring occurrences | |||||
substr_replace() |
Replace text within a portion of a string | |||||
substr() |
Return part of a string | |||||
trim() |
Strip whitespace (or other characters) from the beginning and end of a string | |||||
ucfirst() |
Make a string's first character uppercase | |||||
ucwords() |
Uppercase the first character of each word in a string | |||||
utf8_decode() |
Converts a string from UTF-8 to ISO-8859-1, replacing invalid or unrepresentable characters | |||||
utf8_encode() |
Converts a string from ISO-8859-1 to UTF-8 | |||||
vfprintf() |
Write a formatted string to a stream. | |||||
vprintf() |
Output a formatted string | |||||
vsprintf() |
Return a formatted string | |||||
wordwrap() |
Wrap a string to a given number of characters. | |||||
Splitting/Joining
| fx() name | description |
|---|---|
| chunk_split | Split a string into smaller chunks |
| explode | Split a string by a string |
| implode | Join array elements with a string |
| join | Alias of implode |
Single Character
| fx() name | description | category |
|---|---|---|
| chr() | Generate a single-byte string from a number | conversion |
| lcfirst() | Make a string's first character lowercase | conversion |
| ctype_alnum() | Check for alphanumeric character(s) | ctype |
| ctype_alpha() | Check for alphabetic character(s) | ctype |
| ctype_cntrl() | Check for control character(s) | ctype |
| ctype_digit() | Check for numeric character(s) | ctype |
| ctype_graph() | Check for any printable character(s) except space | ctype |
| ctype_lower() | Check for lowercase character(s) | ctype |
| ctype_print() | Check for printable character(s) | ctype |
| ctype_punct() | Check for any printable character which is not whitespace or an alphanumeric character | ctype |
| ctype_space() | Check for whitespace character(s) | ctype |
| ctype_upper() | Check for uppercase character(s) | ctype |
| ctype_xdigit() | Check for character(s) representing a hexadecimal digit | ctype |
Fuzzy
| fx() name | description |
|---|---|
| levenshtein | Calculate Levenshtein distance between two strings |
| metaphone | Calculate the metaphone key of a string |
Conversion
| fx() name | description | conversion format |
|---|---|---|
| bin2hex | Convert binary data into hexadecimal representation | hexadecimal |
| convert_cyr_string | Convert from one Cyrillic character set to another | Cyrillic |
| convert_uudecode | Decode a uuencoded string | Template:L/wp |
| convert_uuencode | Uuencode a string | Template:L/wp |
| get_html_translation_table | Returns the translation table used by htmlspecialchars and htmlentities | HTML |
| hebrev | Convert logical Hebrew text to visual text | Hebrew |
| hebrevc | Convert logical Hebrew text to visual text with newline conversion | Hebrew |
| hex2bin | Decodes a hexadecimally encoded binary string | hexadecimal |
| html_entity_decode | Convert HTML entities to their corresponding characters | HTML |
| htmlentities | Convert all applicable characters to HTML entities | HTML |
| htmlspecialchars_decode | Convert special HTML entities back to characters | HTML |
| htmlspecialchars | Convert special characters to HTML entities | HTML |
| nl_langinfo — Query language and locale information | locale |
