PHP String Manipulation: Common string functions (strlen, strpos, str_replace, etc.).
Here's a quick rundown of some common string manipulation functions in PHP, which are essential for handling text and data processing.
1. strlen() - Get the Length of a String
The strlen()
function returns the length of a string.
$string = "Hello, World!";echo strlen($string); // Output: 13
2. strpos() - Find Position of First Occurrence of a Substring
The strpos()
function finds the position of the first occurrence of a substring within a string. It returns false
if the substring is not found.
$string = "Hello, World!";$position = strpos($string, "World");echo $position; // Output: 7
3. str_replace() - Replace All Occurrences of a Search String with a Replacement
The str_replace()
function replaces all occurrences of a specified string with another string.
$text = "Hello, World!";$replacedText = str_replace("World", "PHP", $text);echo $replacedText; // Output: "Hello, PHP!"
4. substr() - Extract a Portion of a String
The substr()
function extracts a portion of a string. You can specify the start position and optionally the length.
$string = "Hello, World!";$subString = substr($string, 7, 5); // Start at position 7, get 5 charactersecho $subString; // Output: "World"
5. strtoupper() and strtolower() - Convert String to Uppercase or Lowercase
The strtoupper()
function converts a string to uppercase, while strtolower()
converts it to lowercase.
$string = "Hello, World!";echo strtoupper($string); // Output: "HELLO, WORLD!"echo strtolower($string); // Output: "hello, world!"
6. ucfirst() and ucwords() - Capitalize First Letter(s)
ucfirst()
capitalizes the first letter of a string.ucwords()
capitalizes the first letter of each word in a string.
$text = "hello, world!";echo ucfirst($text); // Output: "Hello, world!"echo ucwords($text); // Output: "Hello, World!"
7. trim() - Remove Whitespace from Both Sides of a String
The trim()
function removes whitespace (or other specified characters) from the beginning and end of a string.
$string = " Hello, World! ";echo trim($string); // Output: "Hello, World!"
8. strrev() - Reverse a String
The strrev()
function reverses the characters in a string.
$string = "Hello, World!";echo strrev($string); // Output: "!dlroW ,olleH"
9. strcmp() - Compare Two Strings
The strcmp()
function compares two strings. It returns 0
if both strings are equal, a negative number if the first string is less than the second, and a positive number if the first string is greater.
$string1 = "Hello";$string2 = "World";echo strcmp($string1, $string2); // Output: negative number (since "Hello" < "World")
10. explode() - Split a String by a Delimiter
The explode()
function splits a string into an array based on a specified delimiter.
$string = "apple,banana,orange";$array = explode(",", $string);print_r($array); // Output: Array ( [0] => apple [1] => banana [2] => orange )
11. implode() - Join Array Elements into a String
The implode()
function joins array elements into a single string with a specified delimiter.
$array = ["apple", "banana", "orange"];$string = implode(", ", $array);echo $string; // Output: "apple, banana, orange"
These functions cover a range of basic string operations commonly used in PHP development.