PHP string Methods
Quick Links
PHP String Length Calculation Methods
strlen():
Returns the length of a string.
$string = "Hello, World!";
$length = strlen($string);
echo $length; // Output: 13
PHP String Trimming Methods
trim():
Removes whitespace or other characters from the beginning and end of a string.
$string = " Hello, World! ";
$trimmed = trim($string);
echo $trimmed; // Output: "Hello, World!"
ltrim():
Removes whitespace or other characters from the beginning of a string.
$string = " Hello, World! ";
$trimmed = ltrim($string);
echo $trimmed; // Output: "Hello, World! "
rtrim():
Removes whitespace or other characters from the end of a string.
$string = " Hello, World! ";
$trimmed = rtrim($string);
echo $trimmed; // Output: " Hello, World!"
PHP Substring Manipulation Methods
substr():
Returns a part of a string.
$string = "Hello, World!";
$substring = substr($string, 7);
echo $substring; // Output: "World!"
substr_replace():
Replaces a part of a string with another string.
$string = "Hello, World!";
$replaced_string = substr_replace($string, "Universe", 7);
echo $replaced_string; // Output: "Hello, Universe!"
PHP Case Conversion Methods
strtolower():
Converts a string to lowercase.
$string = "Hello, World!";
$lowercase_string = strtolower($string);
echo $lowercase_string; // Output: "hello, world!"
strtoupper():
Converts a string to uppercase.
$string = "Hello, World!";
$uppercase_string = strtoupper($string);
echo $uppercase_string; // Output: "HELLO, WORLD!"
ucfirst():
Converts the first character of a string to uppercase.
$string = "hello, world!";
$capitalized_string = ucfirst($string);
echo $capitalized_string; // Output: "Hello, world!"
lcfirst():
Converts the first character of a string to lowercase.
$string = "Hello, World!";
$lowercased_first_string = lcfirst($string);
echo $lowercased_first_string; // Output: "hello, World!"
PHP String Reversal Methods
strrev():
Reverses a string.
$string = "Hello, World!";
$reversed_string = strrev($string);
echo $reversed_string; // Output: "!dlroW ,olleH"
PHP String Searching Methods
strpos():
Finds the position of the first occurrence of a substring in a string.
$string = "Hello, World!";
$position = strpos($string, ",");
echo $position; // Output: 5
stripos():
Finds the position of the first occurrence of a case-insensitive substring in a string.
$string = "Hello, World!";
$position = stripos($string, "WORLD");
echo $position; // Output: 7
strripos():
Finds the position of the last occurrence of a case-insensitive substring in a string.
$string = "Hello, World! Hello, Universe!";
$position = strripos($string, "HELLO");
echo $position; // Output: 13
strstr():
Finds the first occurrence of a string within another string.
$string = "Hello, World!";
$substring = strstr($string, "World");
echo $substring; // Output: "World!"
strchr():
Alias of strstr().
PHP String Repeating and Shuffling Methods
str_shuffle():
Randomly shuffles all characters of a string.
$string = "Hello, World!";
$shuffled_string = str_shuffle($string);
echo $shuffled_string; // Output: "rHloo,W l!odle"
str_repeat():
Repeats a string a specified number of times.
$string = "Hello, ";
$repeated_string = str_repeat($string, 3);
echo $repeated_string; // Output: "Hello, Hello, Hello, "
Great info on the string methods!
ReplyDeleteI created a similar page on sorting algorithms, dynamic programming and more. I particularly liked the visualization for Merge Sort. Would love to hear your thoughts on it:
My Blog: https://algoguide.weebly.com/blog