String Operations in PHP

From the PHP cheat sheet · String Manipulation · verified Jul 2026

String Operations

Common string manipulation functions

php
// String length and case
$length = strlen($string);
$upper = strtoupper($string);
$lower = strtolower($string);

// String search
$pos = strpos($haystack, 'needle');
$contains = str_contains($text, 'word');

// String manipulation
$trimmed = trim($string);
$replaced = str_replace('old', 'new', $string);
$substring = substr($string, 0, 10);

// Explode and implode
$parts = explode(',', $csv);
$joined = implode(' | ', $array);
💡 Use mb_* functions for multibyte (UTF-8) strings
⚡ str_contains/starts_with/ends_with are PHP 8.0+ additions
📌 Always use htmlspecialchars() for user input in HTML
🔥 Heredoc parses variables, Nowdoc does not
Back to the full PHP cheat sheet