Lesson 5 of 8
String Functions - Manipulation & Searching
PHP has over 100 built-in string functions. Knowing the right function for the right job - and choosing mb_ (multibyte) variants for Unicode content - is critical for building internationalized applications. The mb_ functions handle UTF-8 correctly where regular string functions fail.
Essential String Functions
<?php
declare(strict_types=1);
$text = " Hello, PHP World! ";
// ── Whitespace & case ──────────────────────────────────────────
echo trim($text); // "Hello, PHP World!"
echo ltrim($text); // left trim only
echo rtrim($text); // right trim only
echo strtolower("PHP"); // "php"
echo strtoupper("php"); // "PHP"
echo ucfirst("hello world"); // "Hello world"
echo ucwords("hello world php"); // "Hello World Php"
// ── Length & character access ─────────────────────────────────
echo strlen("Hello"); // 5 (bytes)
echo mb_strlen("Héllo"); // 5 (characters - use mb_* for UTF-8!)
echo $text[0]; // " " (first byte)
echo mb_substr("Hello", 1, 3); // "ell" (mb_substr for UTF-8)
// ── Searching ────────────────────────────────────────────────
$haystack = "The quick brown fox";
// PHP 8.0+ - readable modern functions:
str_contains($haystack, "quick"); // true
str_starts_with($haystack, "The"); // true
str_ends_with($haystack, "fox"); // true
// Position
$pos = strpos($haystack, "quick"); // 4 (false if not found)
if ($pos !== false) echo "Found at $pos";
strrpos($haystack, "o"); // 17 (last occurrence)
// ── Replace ───────────────────────────────────────────────────
str_replace("fox", "cat", $haystack); // "The quick brown cat"
str_ireplace("QUICK", "slow", $haystack); // case-insensitive replace
substr_replace($haystack, "cat", 16, 3); // replace 3 chars at pos 16
// Multiple replacements
$clean = str_replace(
["<script>", "</script>", "SELECT", "DROP"],
"",
$userInput
);
// ── Split & join ─────────────────────────────────────────────
$csv = "php,web,backend,api";
$tags = explode(",", $csv); // ["php","web","backend","api"]
$limited = explode(",", $csv, 3); // ["php","web","backend,api"] - max 3
$joined = implode(" | ", $tags); // "php | web | backend | api"
$words = str_word_count("Hello World", 1); // ["Hello", "World"]
// ── Format & pad ─────────────────────────────────────────────
$money = number_format(1234567.891, 2, ".", ","); // "1,234,567.89"
$padded = str_pad("42", 5, "0", STR_PAD_LEFT); // "00042"
$repeat = str_repeat("-", 30); // "------------------------------"
// ── Hashing ──────────────────────────────────────────────────
md5("hello"); // 32-char hex - NOT for passwords
sha1("hello"); // 40-char hex - NOT for passwords
// For passwords: password_hash() - Module 7
hash("sha256", "hello"); // 64-char hex - use for file checksums
// ── mb_* - always use for UTF-8 content ─────────────────────
$emoji = "Hello 🌍";
strlen($emoji); // 10 (bytes)
mb_strlen($emoji); // 7 (characters)
mb_strtolower("Héllo"); // "héllo" (correct)
strtolower("Héllo"); // "hÉllo" (wrong on some systems)Quick Quiz
Tip
Tip
Practice String Functions Manipulation Searching in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for String Functions - Manipulation & Searching
Practice Task
Note
Practice Task - (1) Write a working example of String Functions Manipulation Searching from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with String Functions Manipulation Searching is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?