PHP String Functions

PHP ships with a large library of built-in functions for searching, transforming, and formatting strings.

Why built-in string functions matter

Most real programs spend a great deal of time working with text: cleaning user input, building messages, searching for keywords, and formatting output. Rather than writing this logic by hand, PHP provides a rich set of string functions that are fast, well tested, and consistent. Learning the common ones will save you time and make your code easier to read.

FunctionPurposeExample result
strlen()Returns the number of bytes in a stringstrlen("hello") returns 5
strtoupper()Converts a string to upper casestrtoupper("hi") returns "HI"
strtolower()Converts a string to lower casestrtolower("Hi") returns "hi"
ucfirst()Capitalises the first characterucfirst("hello") returns "Hello"
trim()Removes whitespace from both endstrim(" hi ") returns "hi"
str_replace()Replaces all matches of a substringstr_replace("a", "o", "cat") returns "cot"
strpos()Finds the position of a substringstrpos("hello", "l") returns 2
substr()Extracts part of a stringsubstr("hello", 0, 3) returns "hel"
str_repeat()Repeats a string a number of timesstr_repeat("ab", 3) returns "ababab"

Changing case and trimming

Case functions are useful when you want to compare text without worrying about capitalisation, or when you want to present names and titles consistently. The trim() function is essential for cleaning user input, because form fields often contain accidental leading or trailing spaces that would otherwise cause failed comparisons.

Case conversion and trimming input

<?php
$raw = "   Hello World   ";

echo strtoupper($raw);   //    HELLO WORLD
echo "\n";
echo trim($raw);         // Hello World
echo "\n";
echo ucfirst("php is fun"); // Php is fun
?>

Searching and extracting

The strpos() function tells you where a substring first appears, returning the zero-based position or the boolean false if it is not found. Once you know a position, substr() lets you pull out just the part you want. Because strpos() can return 0 for a match at the very start, always compare its result to false with the strict !== operator.

Finding and slicing text

<?php
$email = "user@example.com";

$atPosition = strpos($email, "@");

if ($atPosition !== false) {
    $username = substr($email, 0, $atPosition);
    echo $username; // user
}
?>
Note: strpos() returns 0 when the match is at the start of the string, and 0 is loosely equal to false. Use the strict comparison !== false so a valid position of 0 is not mistaken for 'not found'.

Replacing text

The str_replace() function scans a string and swaps every occurrence of a search term for a replacement. It is commonly used to sanitise text, build templates, or reformat data. It can also accept arrays, letting you perform several replacements in a single call.

Replacing substrings

<?php
$template = "Hello {name}, welcome to {site}";

$message = str_replace(
    ["{name}", "{site}"],
    ["Ada", "Hyring"],
    $template
);

echo $message; // Hello Ada, welcome to Hyring
?>
Note: Many string functions have multibyte-aware versions prefixed with mb_, such as mb_strtoupper() and mb_substr(). Use those when your text may contain non-ASCII characters so that case changes and slicing respect character boundaries.