Manipulating and Searching Strings in PHP
Manipulating and searching strings in PHP involves changing, extracting, or locating text within strings. Functions like strlen(), strtoupper(), and str_replace() help modify strings, while strpos(), str_contains(), and strstr() assist in finding text. These functions make text handling simple and efficient.
String Manipulation Functions
1. strlen() – Get String Length
Returns the total number of characters in a string, including spaces and special characters. Useful for validating input length.
$text = "Hello World";
echo strlen($text); // Outputs: 11
2. strtoupper() and strtolower()
Converts all characters in a string to uppercase, making it helpful for case-insensitive comparisons or formatting.
strtoupper()converts to uppercase.strtolower()converts to lowercase.
echo strtoupper("hello"); // Outputs: HELLO
echo strtolower("HELLO"); // Outputs: hello
3. ucfirst() and ucwords() – Capitalize Words
ucfirst()Capitalizes the first character of a string, often used for proper names or sentence formatting.ucwords()Capitalizes the first letter of each word in a string, useful for titles or headings.
echo ucfirst("hello world"); // Outputs: Hello world
echo ucwords("hello world"); // Outputs: Hello World
4. str_replace() – Replace Text
Searches for a substring and replaces it with another, commonly used for text corrections or filtering.
echo str_replace("world", "PHP", "Hello world"); // Outputs: Hello PHP
5.substr() – Extract Part of String
Extracts a specific portion of a string based on a starting position and optional length, ideal for slicing text.
$text = "Hello World";
echo substr($text, 0, 5); // Outputs: Hello
echo substr($text, -5); // Outputs: World
6. strrev() – Reverse a String
Reverses the characters in a string, often used for simple text transformations or checks.
echo strrev("PHP"); // Outputs: PHP
String Searching Functions
1. strpos() – Find Position of Text
Finds the first occurrence of a substring. Returns the position (0-based) or false if not found.
echo strpos("Hello World", "World"); // Outputs: 6
2.strrpos() – Find Last Occurrence
Returns the position of the last occurrence of a substring, helpful when searching from the end of a string.
echo strrpos("Hello Hello", "Hello"); // Outputs: 6
3.stripos() and strripos() – Case-Insensitive Search
stripos()– Finds first occurrence (ignores case).strripos()– Finds last occurrence (ignores case).
echo stripos("Hello World", "world"); // Outputs: 6
4.strstr() – Get Substring from First Occurrence
Returns the part of the string starting from a specified substring, often used for extracting relevant text.
echo strstr("Hello World", "World"); // Outputs: World
5.str_contains() – Check If String Contains Text (PHP 8+)
Checks if a string contains a certain substring (PHP 8+), returning true or false, ideal for quick checks.
var_dump(str_contains("Hello World", "World")); // true
6.str_starts_with() and str_ends_with() (PHP 8+)
str_starts_with()– Checks if a string starts with a substring.str_ends_with()– Checks if a string ends with a substring.
var_dump(str_starts_with("Hello World", "Hello")); // true
var_dump(str_ends_with("Hello World", "World")); // true