📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners String Functions

String Functions

5 min read Quiz at the end
PHP has built-in functions to work with text easily. strlen(), strtoupper(), str_replace(), and explode() help you measure, convert, search, and split strings. These save a lot of time when handling user input and text data.

String Functions

$s = "  Hello, PHP World!  ";

strlen($s);                  // length
strtoupper($s);              // HELLO, PHP WORLD!
strtolower($s);              // hello, php world!
trim($s);                    // remove whitespace
ltrim($s); rtrim($s);        // left / right trim
str_replace("PHP","Python",$s); // replace
strpos($s, "PHP");           // find position (or false)
substr($s, 7, 3);            // "PHP"
str_repeat("ha", 3);         // "hahaha"
str_word_count($s);          // word count
str_split($s, 3);            // split into chunks
chunk_split($s, 3, "-");     // "Hel-lo,-PHP-..."
wordwrap($s, 15, "\n", true);// wrap long lines
nl2br("line1\nline2");       // add <br> tags
htmlspecialchars("<b>"); // escape HTML
strip_tags("<b>Bold</b>"); // remove HTML
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which function finds the position of a substring?
💡 strpos() finds the position of the first occurrence of a substring.
2. What does htmlspecialchars() do?
💡 htmlspecialchars() converts <, >, &, " to HTML entities — essential for XSS prevention.
3. Which function splits a string into an array?
💡 str_split() splits by character length; explode() splits by delimiter.
4. What does trim() do?
💡 trim() removes whitespace (or specified chars) from both ends of a string.
5. Which function searches and replaces in a string?
💡 str_replace(search, replace, subject) replaces all occurrences.