PHP String Functions

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().
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:
$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 functionsAdd or remove backslashes before quotes and special characters.
| Function | Description | Tutorial |
|---|---|---|
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 functionsBreak strings apart, merge arrays, parse query strings, and trim whitespace.
| Function | Description | Tutorial |
|---|---|---|
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 functionsEncode and decode HTML for safe output.
| Function | Description | Tutorial |
|---|---|---|
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 functionsConvert between binary, hex, uuencode, quoted-printable, and UTF-8 formats.
| Function | Description | Tutorial |
|---|---|---|
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 functionsMap code points, measure length, count characters, and compare similarity.
| Function | Description | Tutorial |
|---|---|---|
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 functionsChecksums and one-way hashes—prefer modern password APIs when hashing passwords.
Search & position
14 functionsFind substrings, positions, and prefixes or suffixes.
| Function | Description | Tutorial |
|---|---|---|
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 functionsCompare strings by bytes, case, natural order, or locale.
| Function | Description | Tutorial |
|---|---|---|
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 functionsReplace, extract, pad, repeat, shuffle, and reverse strings.
| Function | Description | Tutorial |
|---|---|---|
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 functionsChange letter case and capitalize words.
| Function | Description | Tutorial |
|---|---|---|
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 functionsPrint, format, scan, and wrap strings; write formatted output to streams and files.
| Function | Description | Tutorial |
|---|---|---|
print | Output 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 functionsLocale-aware settings and language information.
| Function | Description | Tutorial |
|---|---|---|
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 functionsStep alphanumeric strings forward or backward (PHP 8.3+).
| Function | Description | Tutorial |
|---|---|---|
str_increment() | Increment an alphanumeric string. | Try it |
str_decrement() | Decrement an alphanumeric string. | Try it |
Legacy & specialized
2 functionsHebrew layout helpers—check deprecation notes before use.
Language construct
1 functionOutput constructs that are part of the PHP language, not string functions.
Frequently Asked Questions
Start with explode()
Open the split-string guide—syntax, examples, and FAQs.
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.
15 people found this page helpful
