PHP String Functions

Beginner
6 min read
Updated: Nov 2026
105 function guides
Function index

About This Index

This page is the PHP String Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.

Learn what a string is on /php/string. Use this page when you need a specific helper such as explode(), implode(), or htmlspecialchars().

Beginner path.

Start with explode(), implode(), and htmlspecialchars()—then browse the rest by category.

How to Call a String Function

PHP string helpers are global functions—pass the string as an argument:

PHP
$text = 'Hello, PHP!';
$csv  = 'a,b,c';

$len   = strlen($text);
$parts = explode(',', $csv);
$joined = implode(' | ', $parts);
$safe  = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');

PHP String Functions Table

All function guides in this PHP tutorial series (105 tutorials).

Escape & slash

5 functions

Add or remove backslashes before quotes and special characters.

FunctionDescriptionTutorial
addcslashes()Escape characters listed in charlist.Try it
addslashes()Escape quotes, backslash, and NUL.Try it
stripcslashes()Unescape C-style escape sequences.Try it
stripslashes()Remove backslashes added by addslashes().Try it
quotemeta()Escape regex metacharacters.Try it

Split, join & trim

12 functions

Break strings apart, merge arrays, parse query strings, and trim whitespace.

FunctionDescriptionTutorial
explode()Split a string into an array.Try it
implode()Join array elements with glue.Try it
join()Alias of implode().Try it
chunk_split()Split into fixed-size chunks.Try it
chop()Remove trailing characters (rtrim alias).Try it
ltrim()Trim characters from the start of a string.Try it
rtrim()Trim characters from the end of a string.Try it
trim()Trim characters from both ends.Try it
strtok()Tokenize a string by delimiters.Try it
str_split()Split a string into character chunks.Try it
str_getcsv()Parse a CSV line into an array.Try it
parse_str()Parse a query string into variables.Try it

HTML & entities

7 functions

Encode and decode HTML for safe output.

FunctionDescriptionTutorial
htmlspecialchars()Escape & < > " ' for HTML.Try it
htmlspecialchars_decode()Reverse htmlspecialchars().Try it
htmlentities()Convert applicable chars to entities.Try it
html_entity_decode()Decode HTML entities.Try it
get_html_translation_table()Entity translation arrays.Try it
strip_tags()Remove HTML and PHP tags.Try it
nl2br()Insert <br> before newlines.Try it

Encode & decode

8 functions

Convert between binary, hex, uuencode, quoted-printable, and UTF-8 formats.

FunctionDescriptionTutorial
bin2hex()Binary string to hexadecimal.Try it
hex2bin()Hexadecimal string to binary.Try it
convert_uuencode()Encode binary as uuencoded text.Try it
convert_uudecode()Decode uuencoded string.Try it
quoted_printable_encode()Encode as quoted-printable.Try it
quoted_printable_decode()Decode quoted-printable text.Try it
utf8_encode()ISO-8859-1 to UTF-8 (deprecated).Try it
utf8_decode()UTF-8 to ISO-8859-1 (deprecated).Try it

Character & analysis

10 functions

Map code points, measure length, count characters, and compare similarity.

FunctionDescriptionTutorial
chr()Code point to character.Try it
ord()Character to byte value.Try it
strlen()Get string length in bytes.Try it
count_chars()Count character frequencies.Try it
convert_cyr_string()Convert between Cyrillic charsets.Try it
str_word_count()Count or list words in a string.Try it
similar_text()Similarity between two strings.Try it
levenshtein()Edit distance between two strings.Try it
metaphone()Metaphone phonetic key.Try it
soundex()Soundex phonetic key.Try it

Hash & checksum

6 functions

Checksums and one-way hashes—prefer modern password APIs when hashing passwords.

FunctionDescriptionTutorial
crc32()32-bit CRC checksum.Try it
crypt()One-way string hashing (legacy).Try it
md5()MD5 hash of a string.Try it
md5_file()MD5 hash of a file.Try it
sha1()SHA-1 hash of a string.Try it
sha1_file()SHA-1 hash of a file.Try it

Search & position

14 functions

Find substrings, positions, and prefixes or suffixes.

FunctionDescriptionTutorial
strpos()Position of first occurrence.Try it
stripos()Case-insensitive strpos().Try it
strrpos()Position of last occurrence.Try it
strripos()Case-insensitive strrpos().Try it
strstr()Find first occurrence and return the rest.Try it
stristr()Case-insensitive strstr().Try it
strchr()Alias of strstr().Try it
strrchr()Find last occurrence of a character.Try it
strpbrk()Search for any of several characters.Try it
strspn()Length of initial segment matching a mask.Try it
strcspn()Length of initial segment not matching a mask.Try it
str_contains()Check whether a string contains a substring.Try it
str_starts_with()Check whether a string starts with a prefix.Try it
str_ends_with()Check whether a string ends with a suffix.Try it

Compare

8 functions

Compare strings by bytes, case, natural order, or locale.

FunctionDescriptionTutorial
strcmp()Binary-safe string comparison.Try it
strcasecmp()Case-insensitive comparison.Try it
strncmp()Compare the first n characters.Try it
strncasecmp()Case-insensitive compare of first n characters.Try it
strnatcmp()Natural-order comparison.Try it
strnatcasecmp()Case-insensitive natural-order comparison.Try it
strcoll()Locale-based string comparison.Try it
substr_compare()Compare substrings from an offset.Try it

Replace & modify

11 functions

Replace, extract, pad, repeat, shuffle, and reverse strings.

FunctionDescriptionTutorial
str_replace()Replace all occurrences of a search string.Try it
str_ireplace()Case-insensitive str_replace().Try it
substr_replace()Replace part of a string by position.Try it
strtr()Translate characters or substrings.Try it
str_pad()Pad a string to a given length.Try it
str_repeat()Repeat a string n times.Try it
str_shuffle()Randomly shuffle characters.Try it
str_rot13()Apply ROT13 transformation.Try it
strrev()Reverse a string.Try it
substr()Return part of a string.Try it
substr_count()Count substring occurrences.Try it

Case & words

5 functions

Change letter case and capitalize words.

FunctionDescriptionTutorial
strtolower()Convert to lowercase.Try it
strtoupper()Convert to uppercase.Try it
lcfirst()Lowercase the first character.Try it
ucfirst()Uppercase the first character.Try it
ucwords()Uppercase the first letter of each word.Try it

Format & output

11 functions

Print, format, scan, and wrap strings; write formatted output to streams and files.

FunctionDescriptionTutorial
printOutput a string (language construct).Try it
printf()Output a formatted string.Try it
sprintf()Return a formatted string.Try it
sscanf()Parse input according to a format.Try it
fprintf()Format and write to a file handle.Try it
vprintf()Output a formatted string from an array.Try it
vsprintf()Return a formatted string from an array.Try it
vfprintf()Write a formatted array string to a stream.Try it
number_format()Format a number with separators.Try it
money_format()Format a number as currency (removed in PHP 8).Try it
wordwrap()Wrap text at a line length.Try it

Locale

3 functions

Locale-aware settings and language information.

FunctionDescriptionTutorial
setlocale()Set locale information.Try it
localeconv()Numeric formatting conventions for the locale.Try it
nl_langinfo()Query language and locale information.Try it

Increment & decrement

2 functions

Step alphanumeric strings forward or backward (PHP 8.3+).

FunctionDescriptionTutorial
str_increment()Increment an alphanumeric string.Try it
str_decrement()Decrement an alphanumeric string.Try it

Legacy & specialized

2 functions

Hebrew layout helpers—check deprecation notes before use.

FunctionDescriptionTutorial
hebrev()Convert Hebrew text for display.Try it
hebrevc()hebrev() plus line breaks (deprecated).Try it

Language construct

1 function

Output constructs that are part of the PHP language, not string functions.

FunctionDescriptionTutorial
echoOutput one or more strings (language construct).Try it

Frequently Asked Questions

PHP string functions are built-in helpers for text: escaping, splitting CSV, joining arrays, encoding HTML, converting hex, and more. You call them by name: strlen($text) or htmlspecialchars($html).
The Strings page teaches what a string is—quotes, the . operator, and strlen(). This Functions index lists every function tutorial so you can open explode(), implode(), htmlspecialchars(), and the rest.
Use function_name(arguments). The string is usually the first parameter: explode(",", $line), htmlspecialchars($input, ENT_QUOTES, "UTF-8"), implode("-", $parts).
explode() splits a string into an array. implode() (alias join()) joins array elements into one string. They are opposites.
If you are new, read PHP Strings first. Then open explode(), implode(), and htmlspecialchars() from the table below.

Start with explode()

Open the split-string guide—syntax, examples, and FAQs.

explode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful