Code has been added to clipboard!

PHP String Functions Made Easy: Handle Strings in a More Efficient Way

Reading time 7 min
Published Aug 8, 2017
Updated Oct 2, 2019

Out of all eight data types, PHP strings is considered one of the simplest, along with integers, floats, and booleans.

There are built-in PHP string functions for manipulating strings. One of the common uses is to use one of the PHP functions to split the string into smaller parts if the length of a string. You can also use them to have a specific PHP string replaced, modified, or perform other actions.

PHP String Functions: Main Tips

  • As the naming suggests, PHP string functions help to perform different actions on string data.
  • They are integrated into the core of PHP.
  • Among the most basic and widely used are PHP compare strings and PHP trim string functions.

List of Methods

Let's see the complete list of PHP string functions and their descriptions. The table is organized in alphabetical order, so you can always find a particular one quickly when you need to, say, have a certain PHP string replaced:

Function Description
addcslashes() Makes a new string by adding backslashes to the front of each specified character
addslashes() Makes a new string by adding backslashes to the front of predefined characters
bin2hex() Makes a new string by converting every character of a string to hexadecimal values
chop() Makes a new string by removing whitespace and other unnecessary characters from the right side of a string
chr() Displays a character defined by a specified ASCII value
chunk_split() Splits the string into smaller parts
convert_cyr_string() Converts a string from one Cyrillic character-set to another
convert_uudecode() Decodes a uuencoded string
convert_uuencode() Makes a new string by converting using the uuencode algorithm
count_chars() Gives information about characters that the string contains
crc32() Calculates a 32-bit CRC for a string
crypt() Makes a one-way hashing of a string
echo() Displays the specified string(s)
explode() Splits a string into array elements
fprintf() Writes the string into specified output stream
get_html_translation_table() Returns the translation table used by htmlspecialchars() and htmlentities()
hebrev() Hebrew text is converted to plain text
hebrevc() Converts Hebrew text to visual text and new lines (\n) into <br>
hex2bin() Makes a new string by converting the specified string of hexadecimal characters to ASCII characters
html_entity_decode() Makes a new string by converting HTML elements to characters
htmlentities() Makes a new string by converting plain text to HTML entities
htmlspecialchars_decode() Makes a new string by converting predefined HTML elements to characters
htmlspecialchars() Makes a new string by converting predefined characters to HTML elements
implode() Joins a PHP array to string
join() Alternative to implode()
lcfirst() Makes a new string by converting the first letter of the specified string to lowercase
levenshtein() Returns the Levenshtein distance between two PHP strings
localeconv() Returns information about locale numeric and monetary formatting
ltrim() Makes a new string by removing whitespace and other unnecessary characters from the left side of a string
md5() Creates the MD5 hash of a string
md5_file() Creates the MD5 hash of a specified file
metaphone() Creates a metaphone key of a string
money_format() Makes a new string by formatting it as a currency string
nl_langinfo() Returns information about a specific location
nl2br() Puts new HTML line breaks in the front of every newline in a string
number_format() Makes a new string by formatting a number. Groups thousands
ord() Takes the first character of a string and returns its ASCII value
parse_str() Puts a query string into specified variables
print() Displays a specified string or multiple strings
printf() Displays a specified formatted string or multiple strings
quoted_printable_decode() Makes a new string by converting quoted-printable string into an 8-bit string
quoted_printable_encode() Makes a new string by converting an 8-bit string into quoted-printable string
quotemeta() Quotes meta characters
rtrim() Makes a new string by removing whitespace and other unnecessary characters from the right side of a string
setlocale() Sets information about the locale
sha1() Creates a SHA-1 hash for a string
sha1_file() Creates a SHA-1 hash for a file
similar_text() Compares two PHP strings
soundex() Creates a soundex key for a string
sprintf() Takes a formatted string and writes it to a variable
sscanf() Takes input from a string depending on the format
str_getcsv() Takes CSV string and puts it into an array
str_ireplace() Makes a new string by replacing characters of a string. The method is not case-sensitive
str_pad() Makes a new string by padding it to a new PHP string length
str_repeat() Makes a new string by repeating a specified string a set number of times
str_replace() Makes PHP string replace part of its content. The method is case-sensitive
spstr_rot13() Performs the ROT13 encoding on a string
str_shuffle() Makes a new string by randomly shuffling all characters
str_split() Splits PHP string to array
str_word_count() Returns how many wors are in a string
strcasecmp() Compares two specified PHP strings. The method is not case-sensitive
strchr() Searches for the first occurrence of a specified string in another string
strcmp() Compares two specified PHP strings. The method is case-sensitive
strcoll() Compares two specified PHP strings. Comparison is based on locale string
strcspn() Returns a count of characters until a specified character is found
strip_tags() Removes PHP and HTML tags from a string
stripcslashes() Makes a new string by removing quotes that were added using the addcslashes() method
stripslashes() Makes a new string by removing quotes that were added using the addslashes() method
stripos() Searches for the first occurrence of a specified string in another string. Returns the position. The method is not case-sensitive
stristr() Searches for the first occurrence of a specified string in another string. The method is not case-sensitive
strlen() Calculates PHP string length and returns it
strnatcasecmp() Compares two specified PHP strings. Uses natural order algorithm. The method is not case-sensitive
strnatcmp() Compares two specified PHP strings. Uses natural order algorithm. The method is case-sensitive
strncasecmp() Compares two specified PHP strings. Compares only specified number of first characters. The method is not case-sensitive
strncmp() Compares two specified PHP strings. Compares only specified number of first characters. The method is case-sensitive
strpbrk() Searches for any of the specified characters in a given string
strpos() Searches for the first occurrence of a specified string in another string. The method is case-sensitive
strrchr() Searches for the last occurrence of a specified string in another string
strrev() Makes a new string by reversing the given string
strripos() Searches for the last position of a specified string in another string. The method is not case-sensitive
strrpos() Searches for the last position of a specified string in another string. The method is case-sensitive
strspn() Counts the number of characters found in a string that are in a specified charlist
strstr() Searches for the first occurrence of a specified string in another string. The method is case-sensitive
strtok() Splits the specified string into smaller strings
strtolower() Changes all the characters of a string to lowercase letters
strtoupper() Changes all the characters of a string to PHP uppercase letters
strtr() Translates certain characters in a string
substr() Cuts a part of a string and returns it
substr_compare() Compares parts of two PHP strings. The start position and case-sensitivity can be specified
substr_count() Returns how many times a substring occurs in a string
substr_replace() Replaces a part of a string with another string
trim() Makes a new string by removing whitespace and other unnecessary characters from both sides of a string
ucfirst() Changes the first character of a string to PHP uppercase
ucwords() Changes the first letter of every word to PHP uppercase
vfprintf() Puts a formatted string to an output stream
vprintf() Displays a formatted string in an output
vsprintf() Puts a formatted string into a variable
wordwrap() Wraps a given string into specified number of characters

PHP String Functions: Summary

  • When performing various actions using string data, PHP string functions come in handy. For example, you can easily turn PHP array to string or PHP string to array.
  • PHP string functions are inbuilt and don't require any installation.
  • One of the most commonly used methods are applied to PHP compare strings and PHP trim string.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()