PHP String Handling Functions

The string functions allow you to manipulate strings, it's part of the PHP core, and there is no installation required; to use these functions.

Trimming Strings

FunctionDescription
trim()Removes whitespace at beginning and end of a string.
ltrim()Removes whitespace at the beginning of a string.
rtrim()Removes whitespace at the end of a string.

Presentation

FunctionDescription
htmlentities()Escapes all HTML entities.
nl2br()Inserts a <br /> tag before each newline character in a string.
strtoupper()Converts a string to uppercase.
strtolower()Converts a string to lowercase.
ucfirst()Converts the first character of a string to uppercase.
ucwords()Converts the first character of each word in a string to uppercase.

Converting Strings and Arrays

FunctionDescription
explode()Splits a string into an array on a specified character or group of characters.
implode()Converts an array into a string, placing a specified character or group of characters between each array element.
join()Same as implode().

Substrings

FunctionDescription
substr(str,pos)Returns the substring from the character in position pos to the end of the string.
substr(str,-len)Returns the substring from len characters from the end of the string to the end of the string.
substr(str,pos,len)Returns a len length substring beginning with the character in position pos.
substr(str,pos,-len)Returns a substring beginning with the character in position pos and chopping off the last len characters of the string.
strstr(haystack,needle,before_needle)If the third argument (before_needle) is false (default), then it returns the part of the haystack from the needle on.If the third argument (before_needle) is true, then it returns the part of the haystack before the needle.The needle can be a string or an integer (or a number that can be converted to an integer).
stristr(haystack,needle,before_needle)Same as strstr(), but case insensitive.
strpos(haystack,needle)Finds the position of the first occurrence of a specified needle in a haystack (string).The needle can be a string or an integer (or a number that can be converted to an integer).
strrpos(haystack,needle)Finds the position of the last occurrence of a specified needle in a haystack (string).The needle can be a string or an integer (or a number that can be converted to an integer).
str_replace()Replaces all occurrences of one string with another string.

Comparing Strings

FunctionDescription
strcmp()Compares two strings. Returns < 0 if str1 is less than str2, > 0 if str1 is greater than str2, and 0 if they are equal.
strcasecmp()Like strcmp() but case insensitive.
strlen()Returns the length of a string.

More Functions

  • addcslashes - Quote string with slashes in a C style
  • addslashes - Quote string with slashes
  • bin2hex - Convert binary data into hexadecimal representation
  • chop - Alias of rtrim
  • chr - Return a specific character
  • chunk_split - Split a string into smaller chunks
  • convert_cyr_string - Convert from one Cyrillic character set to another
  • convert_uudecode - Decode a uuencoded string
  • convert_uuencode - Uuencode a string
  • count_chars - Return information about characters used in a string
  • crc32 - Calculates the crc32 polynomial of a string
  • crypt - One-way string encryption (hashing)
  • echo - Output one or more strings
  • explode - Split a string by string
  • fprintf - Write a formatted string to a stream
  • get_html_translation_table - Returns the translation table used by htmlspecialchars and htmlentities
  • hebrev - Convert logical Hebrew text to visual text
  • hebrevc - Convert logical Hebrew text to visual text with newline conversion
  • html_entity_decode - Convert all HTML entities to their applicable characters
  • htmlentities - Convert all applicable characters to HTML entities
  • htmlspecialchars_decode - Convert special HTML entities back to characters
  • htmlspecialchars - Convert special characters to HTML entities
  • implode - Join array elements with a string
  • join - Alias of implode
  • lcfirst - Make a string's first character lowercase
  • levenshtein - Calculate the Levenshtein distance between two strings
  • 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
  • metaphone - Calculate the metaphone key of a string
  • money_format - Formats a number as a currency string
  • nl_langinfo - Query language and locale information
  • nl2br - Inserts HTML line breaks before all newlines in a string
  • number_format - Format a number with grouped thousands
  • ord - Return ASCII value of a character
  • 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
  • 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_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 shuffles a string
  • str_split - Convert a string to an array
  • 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 a length of initial segment not matching the mask
  • strip_tags - Strip HTML and PHP tags from a string
  • stripcslashes - Un-quote string quoted with addcslashes
  • stripos - Find position of first occurrence of a case-insensitive string
  • stripslashes - Un-quote string quoted with addslashes
  • stristr - Case-insensitive strstr
  • strlen - Get string length
  • 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 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 string in a string
  • strrpos - Find the position of the last occurrence of a char in a string
  • strspn - Find the length of initial segment matching mask
  • strstr - Find the first occurrence of a string
  • strtok - Tokenize string
  • strtolower - Make a string lowercase
  • strtoupper - Make a string uppercase
  • strtr - Translate certain characters
  • substr_compare - Binary safe comparison of 2 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
  • vfprintf - Write a formatted string to a stream
  • vprintf - Output a formatted string
  • vsprintf - Return a formatted string
  • wordwrap - Wraps a string to a given number of characters

Scroll Back to Top