PHP String Functions

PHP has many built-in functions for manipulating strings. Here are some examples of common string functions:

  1. strlen() – Returns the length of a string.
  2. strtolower() – Converts a string to lowercase.
  3. strtoupper() – Converts a string to uppercase.
  4. substr() – Extracts a portion of a string.
  5. str_replace() – Replaces all occurrences of a search string with a replacement string.
  6. trim() – Removes whitespace from the beginning and end of a string.
  7. strpos() – Finds the position of the first occurrence of a substring in a string.
  8. str_split() – Splits a string into an array.

Here are some examples of how you can use these functions:

$string = 'Hello World';

echo strlen($string); // Outputs 11
echo strtolower($string); // Outputs hello world
echo strtoupper($string); // Outputs HELLO WORLD
echo substr($string, 6); // Outputs World
echo str_replace('World', 'Earth', $string); // Outputs Hello Earth
echo trim(' Hello World '); // Outputs Hello World
echo strpos($string, 'World'); // Outputs 6
print_r(str_split($string, 3)); // Outputs Array ( [0] => Hel [1] => lo [2] => Wor [3] => ld )