PHP String Functions
String Functions
1. String Replacement
str_replace($search, $replace, $subject): Replace all occurrences of a substring with another substring.
php
$text = "Hello, World!";
$new_text = str_replace("World", "Alice", $text); // Hello, Alice!
str_ireplace($search, $replace, $subject): Case-insensitive version of str_replace.
php
$text = "Hello, world!";
$new_text = str_ireplace("WORLD", "Alice", $text); // Hello, Alice!
2. String Trimming
trim($string): Remove whitespace or other characters from the beginning and end of a string.
php
$text = " Hello, World! ";
$trimmed = trim($text); // "Hello, World!"
ltrim($string): Remove whitespace from the beginning of a string.
php
$text = " Hello, World!";
$left_trimmed = ltrim($text); // "Hello, World!"
rtrim($string): Remove whitespace from the end of a string.
php
$text = "Hello, World! ";
$right_trimmed = rtrim($text); // "Hello, World!"
3. Substring Operations
substr($string, $start, $length): Return a portion of the string.
php
$text = "Hello, World!";
$part = substr($text, 7, 5); // World
str_split($string, $length): Split a string into an array of strings.
php
$text = "Hello";
$array = str_split($text, 2); // ['He', 'll', 'o']
4. String Position
strpos($haystack, $needle, $offset): Find the position of the first occurrence of a substring.
php
$text = "Hello, World!";
$position = strpos($text, "World"); // 7
strrpos($haystack, $needle, $offset): Find the position of the last occurrence of a substring.
php
$text = "Hello, World! World!";
$position = strrpos($text, "World"); // 13
stripos($haystack, $needle, $offset): Case-insensitive version of strpos.
php
$text = "Hello, world!";
$position = stripos($text, "WORLD"); // 7
5. String Case Conversion
strtolower($string): Convert all characters to lowercase.
php
$text = "Hello, World!";
$lowercase = strtolower($text); // hello, world!
strtoupper($string): Convert all characters to uppercase.
php
$text = "Hello, World!";
$uppercase = strtoupper($text); // HELLO, WORLD!
ucfirst($string): Capitalize the first character of a string.
php
$text = "hello";
$capitalized = ucfirst($text); // Hello
ucwords($string): Capitalize the first character of each word.
php
$text = "hello world";
$capitalized = ucwords($text); // Hello World
6. String Encoding and Decoding
base64_encode($data): Encode data with MIME base64.
php
$data = "Hello, World!";
$encoded = base64_encode($data); // SGVsbG8sIFdvcmxkIQ==
base64_decode($data): Decode a base64 encoded string.
php
$decoded = base64_decode($encoded); // Hello, World!
urlencode($string): Encode a string to be used in a URL.
php
$text = "Hello, World!";
$encoded = urlencode($text); // Hello,+World!
urldecode($string): Decode a URL-encoded string.
php
$decoded = urldecode($encoded); // Hello, World!
7. String Formatting
sprintf($format, ...): Return a formatted string.
php
$formatted = sprintf("Hello, %s!", "Alice"); // Hello, Alice!
printf($format, ...): Output a formatted string.
php
printf("Hello, %s!", "Alice"); // Hello, Alice!
Regular Expressions
PHP also supports regular expressions, which are used for pattern matching and complex string operations:
preg_match($pattern, $subject, &$matches): Perform a regular expression match.
php
$text = "Hello, World!";
preg_match("/World/", $text, $matches); // $matches[0] is "World"
preg_replace($pattern, $replacement, $subject): Perform a regular expression search and replace.
php
$text = "Hello, World!";
$new_text = preg_replace("/World/", "Alice", $text); // Hello, Alice!
String Functions in PHP 8.0 and Later
str_contains($haystack, $needle): Check if a string contains a given substring.
php
$text = "Hello, World!";
$contains = str_contains($text, "World"); // true
str_starts_with($haystack, $needle): Check if a string starts with a given substring.
php
$text = "Hello, World!";
$starts_with = str_starts_with($text, "Hello"); // true
str_ends_with($haystack, $needle): Check if a string ends with a given substring.
php
$text = "Hello, World!";
$ends_with = str_ends_with($text, "World!"); // true
Summary
Strings in PHP are a powerful and flexible way to handle text. PHP provides numerous functions to manipulate strings, perform searches, replace text, and format output. Mastery of these functions is crucial for effective PHP programming and handling textual data in web applications.