Strings are groups of characters. For example, ‘L’ is a character, and
‘LearnTricking’ is a string.
Installation
These functions do not need any installation. They are part of PHP's core.
Here is a list of PHP string functions.
Example
This program shows how to calculate a 32-bit CRC for the string
“Hello World”
using the PHP function `crc32()`
. It shows results with and
without the `%u`
format.
<?php // PHP program illustrate the // crc32() function $str1 = crc32("Hello world."); // print without %u echo 'Without %u: '.$str1."\n"; // print with %u echo 'With %u: '; printf("%u\n", $str1); ?>
Output
Without %u: 2335835140 With %u: 2335835140
This example helps you understand how the
`crc32()`
function works in
PHP.
PHP String Functions | Description |
---|---|
addcslashes() | Add backslashes before some specified characters in a given string. |
addslashes() | Returns a string with backslashes in front of predefined characters. |
bin2hex() | The conversion is done byte-wise with the high-nibble first. |
chop() | Remove white spaces or any other specified characters from the end of a string. |
chr() | Convert a ASCII value to a character. |
chunk_split() | Split a string into smaller chunks of a specific length. |
convert_uudecode() | Decode a uuencoded string encoded using convert_uuencode() function. |
convert_uuencode() | Encodes a string using the uuencode algorithm. |
count_chars() | Perform several operations related to string like the number of an ASCII character occurs in a string. |
crc32() | This function can be used to validate data integrity. |
crypt() | Returns a hashed string using DES, Blowfish, or MD5 algorithms. (Deprecated, use password_hash instead) |
password_hash() | Returns the hashed string on success or FALSE. |
echo | Display the output of parameters that are passed to it. |
explode() | Explode() function splits a string based on a string delimiter |
hex2bin() | Decode a hexadecimally encoded binary string. |
implode() | Elements of an array. implode() is an alias for PHP join(). |
join() | Join an array of elements that are separated by a string. |
lcfirst() | It takes a string as an argument and converts the first character to lowercase. |
levenshtein() | Calculate the levenshtein distance between two strings |
ltrim() | Removes whitespaces or other characters from the left side of a string. |
md5_file() | Generate the md5 hash value of a given file. |
md5() | This function can take up to a maximum of two parameters (string to hash and optional raw output). |
metaphone() | Calculate the metaphone key of a given string. |
nl2br() | Insert HTML break tags in the place of all new lines in a string. |
number_format() | Format a number with grouped thousands. |
ord() | Returns the ASCII value of the first character of a string |
parse_str() | Parses a query string into variables. |
quoted_printable_decode() | Decode a quoted printable string in PHP into an 8-bit string. |
quoted_printable_encode() | Convert an 8-bit string to a quoted printable string. |
quotemeta() | Accepts a string as an argument and returns a string with backslashes in front of characters that need to be escaped within a regular expression. |
rtrim() | Removes whitespaces or other characters (if specified) from the right side of a string. |
sha1_file() | Returns a string on success and returns FALSE otherwise. |
sha1() | This function can take up to a maximum of two parameters (string to hash and optional raw output). |
similar_text() | Returns the number of matching characters in the two strings. |
soundex() | Calculate the Soundex key of a given string. |
str_pad() | Pad a string to a given length. |
str_repeat() | Create a new string by repeating a given string fixed number of times. |
str_replace() | Replace all the occurrences of the search string or array of search strings with a replacement string or array of replacement strings. |
str_rot13() | Accepts a string and shifts every alphabet present in the string by 13 places in the alphabet. |
str_shuffle() | Randomly shuffle all the characters of a string passed to the function as a parameter. |
str_split() | Convert the given string into an array. |
str_word_count() | Perform several operations related to string like total number words in the string, positions of the words in the string etc. |
strcasecmp() | This is used to compare two given strings in a case-insensitive manner. |
strchr() | Search for the first occurrence of a given string within another string. |
strcmp() | It is used to compare two strings. |
strcoll() | It is used to compare two strings based on their locale. |
strcspn() | Returns the length of the initial segment of a string consisting exclusively of characters not contained in a given string. |
strip_tags() | This is used to strips a string from HTML, and PHP tags. |
stripos() | Find the position of the first occurrence of a string within another string in a case-insensitive manner. |
stripslashes() | Removes backslashes from a string. |
stristr() | It searches for the first occurrence of a string inside another string in a case-insensitive manner and displays the portion from the occurrence till the end of the string. |
strlen() | It takes a string as a parameter and returns its length. |
strnatcasecmp() | This function is similar to strnatcmp() only difference being the case-insensitivity. |
strnatcmp() | Return a positive integer, negative, or zero depending on the result of the comparison. |
strncasecmp() | Compare two given strings up to a specified number of characters. The comparison is case-insensitive. |
strncmp() | Compare the first n characters of two strings. |
strpbrk() | Searches a string for any of the specified characters and returns the part of the string from the beginning to the first occurrence of any character in the search string. |
strpos() | Find the position of the first occurrence of a string within another string. |
strrchr() | This function takes two arguments a string and a character. It returns the last occurrence of the character in the string. |
strrev() | It is used to reverse a string. |
strripos() | Find the position of the last occurrence of a string within another string in a case-insensitive manner. |
strrpos() | Find the position of the last occurrence of a string within another string. |
strspn() | Finds the length of the initial segment of a string consisting exclusively of characters contained in a given string. |
strstr() | It searches for the first occurrence of a string inside another string and displays the portion from the occurrence till the end of the string. |
strtok() | Tokenize a string into smaller parts on the basis of given delimiters. |
strtolower() | Convert a string into lowercase. |
strupper() | Convert a string into uppercase. |
strtr() | Replace a set of characters in a string with another set. |
substr_compare() | Compare two strings from a specified starting position up to a specified length. |
substr_count() | Count the number of times a substring occurs in a given string. |
substr_replace() | Replace a part of a string with another string. |
substr() | That is used to extract a part of a string. |
trim() | Removes whitespaces and also the predefined characters from both sides of a string. |
ucfirst() | Takes a string as an argument and returns the string with the first character converted to uppercase. |
ucwords() | Convert the first character of every word in a string to upper-case. |
vprintf() | Display array values as a formatted string using a specified format string and arguments. |
vsprintf() | Return a formatted string using a specified format string and arguments. |
wordwrap() | Wraps a given string to a given number of characters using a string break character. |