PHP String Functions with example
PHP provides a large number of built-in functions for manipulating strings. Here are a few examples of common string functions in PHP:
strlen()
: This function returns the length of a string.
$str = "Hello World"; echo strlen($str); // Outputs: 11
strtolower()
: This function converts a string to all lowercase letters.
$str = "Hello World";
echo strtolower($str); // Outputs: "hello world"
strtoupper()
: This function converts a string to all uppercase letters.$str = "Hello World";
echo strtoupper($str); // Outputs: "HELLO WORLD"
substr()
: This function returns a portion of a string.$str = "Hello World";
echo substr($str, 0, 5); // Outputs: "Hello"
str_replace()
: This function searches for a specified value in a string and replaces it with a new value.
$str = "Hello World";
echo str_replace("World", "PHP", $str); // Outputs: "Hello PHP"
strpos()
: This function searches for a specified value in a string and returns the position of the first occurrence.$str = "Hello World";
echo strpos($str, "World"); // Outputs: 6