10 Essential PHP String Manipulation Functions with Examples
This article introduces ten essential PHP string manipulation functions—including substr, str_replace, implode, strpos, and sprintf—explaining their purpose and providing clear code examples that demonstrate how to extract, replace, concatenate, search, split, trim, and format strings efficiently in PHP development.
In PHP development, strings are fundamental, and mastering key string functions can greatly improve code efficiency.
Part 1: Substring and Concatenation
$string = "Hello World";
$result = substr($string, 6, 5); // outputs "World"
substr(string $string, int $start[, int $length]) extracts a portion of a string starting at a given position.
$string = "Hello World";
$result = str_replace("World", "PHP", $string); // outputs "Hello PHP"
str_replace(mixed $search, mixed $replace, mixed $subject[, int &$count]) replaces occurrences of a search string with a replacement.
$array = array("Hello", "PHP", "World");
$result = implode(" ", $array); // outputs "Hello PHP World"
implode(string $glue, array $pieces) joins array elements into a single string using a specified separator.
Part 2: Search and Checks
$string = "Hello PHP World";
$result = strpos($string, "PHP"); // outputs 6
strpos(string $haystack, mixed $needle[, int $offset = 0]) returns the position of the first occurrence of a needle.
$result = str_contains($string, "PHP"); // outputs 1 (true)
str_contains(string $haystack, mixed $needle) checks if a string contains a given substring (PHP 8.0+).
$result = str_starts_with($string, "Hello"); // outputs 1 (true)
str_starts_with(string $haystack, mixed $needle) determines whether a string starts with a given prefix (PHP 8.0+).
$result = str_ends_with($string, "World"); // outputs 1 (true)
str_ends_with(string $haystack, mixed $needle) determines whether a string ends with a given suffix (PHP 8.0+).
Part 3: Split and Formatting
$string = "Hello,PHP,World";
$result = explode(",", $string); // outputs array('Hello','PHP','World')
explode(string $delimiter, string $string[, int $limit = PHP_INT_MAX]) splits a string into an array by a delimiter.
$string = " Hello World ";
$result = trim($string); // outputs "Hello World"
trim(string $string[, string $characters = " \t\n\r\0\x0B"]) removes whitespace or specified characters from both ends of a string.
$name = "Alice";
$age = 25;
$result = sprintf("My name is %s and I am %d years old.", $name, $age); // outputs "My name is Alice and I am 25 years old."
sprintf(string $format, mixed ...$args) returns a formatted string according to the given format.
By applying these ten essential functions—substr, str_replace, implode, strpos, str_contains, str_starts_with, str_ends_with, explode, trim, and sprintf—developers can manipulate strings more efficiently, leading to cleaner and more maintainable PHP code.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.