PHP String Functions (with Output)
In this lesson: Most-used PHP string functions — strlen, strtoupper, substr, strpos, explode, implode, str_replace — with output.
Working with text
A lot of web work is about text — names, emails, messages, search terms. PHP gives you many ready-made string functions to measure, change, search and split text without writing loops yourself. Learn a handful of these and most text tasks become one line.
Length and case
<?php
$t = "Coding Easily";
echo strlen($t); // 13 (number of characters)
echo strtoupper($t); // CODING EASILY
echo strtolower($t); // coding easily
echo ucwords("hello world"); // Hello World
?>
strlen counts characters, strtoupper/strtolower change case, and ucwords capitalises the first letter of each word.
Search and replace
<?php
$t = "I like PHP";
echo str_replace("PHP", "MySQL", $t); // I like MySQL
echo strpos($t, "PHP"); // 7 (position where PHP starts)
?>
7
str_replace swaps text; strpos tells you where a word starts (or returns false if not found). Because 0 is a valid position, check strpos with === false.
Trim and substring
<?php
$name = " Aman ";
echo trim($name); // "Aman" (spaces removed)
echo substr("CodingEasily", 0, 6); // Coding
?>
Coding
trim removes stray spaces from both ends — essential for cleaning form input. substr pulls out a part: start at 0, take 6 characters.
explode and implode
These convert between a string and an array — very useful for CSV-like data.
<?php
$csv = "apple,mango,banana";
$arr = explode(",", $csv); // ["apple","mango","banana"]
echo $arr[1]; // mango
$back = implode(" | ", $arr);
echo $back; // apple | mango | banana
?>
apple | mango | banana
explode splits a string into an array by a separator; implode joins an array back into a string. They are perfect opposites.
Common mistakes
- Forgetting positions start at 0. The first character is
substr($t, 0, 1), not position 1. - Checking
strposwith==. Since it can return 0 (a valid position), use=== falseto test "not found". - Not trimming form input. Extra spaces cause failed logins and duplicate data.
- Mixing up explode and implode. explode makes an array; implode makes a string.
Summary
strlenfor length;strtoupper/strtolower/ucwordsfor case.str_replaceto replace;strposto find a position.trimcleans spaces;substrpulls out a part.explodesplits to an array;implodejoins to a string.- Positions start at 0 — remember this for substr and strpos.
Next, store split results with arrays in PHP, or clean user input in form validation.
Frequently Asked Questions
How do I find the length of a string in PHP?
strlen($text). It returns the number of characters, so strlen("PHP") gives 3. It is often used to check if input is too short or too long.How do I join and split strings?
explode(",", $csv) to split a string into an array by a separator, and implode(",", $array) to join an array back into a string. They are exact opposites of each other.How do I replace part of a string?
str_replace("old", "new", $text). It finds every "old" and swaps it for "new". It is great for cleaning or reformatting text.What does trim() do?
trim($text) removes spaces (and other whitespace) from the start and end of a string. It is very important for form input, where users often add accidental spaces.How do I get part of a string?
substr($text, start, length). For example substr("CodingEasily", 0, 6) gives "Coding". Counting starts at 0 for the first character.