PHP has a set of built-in functions that you can use to modify strings.
PHP strtoupper() Function
The strtoupper() function returns a string in upper case.
Example
Return the string in upper case:
$x = "Hello World!";
echo strtoupper($x);
PHP strtolower() Function
The strtolower() function returns a string in lower case.
Example
Return the string in lower case:
$x = "Hello World!";
echo strtolower($x);
PHP str_replace() Function
The str_replace() function replaces some characters with some other characters in a string.
Example
Replace the text "World" with "Dolly":
$x = "Hello World!";
echo str_replace("World", "Dolly", $x);
PHP strrev() Function
The strrev() function reverses a string.
Example
Reverse the string "Hello World!":
$x = "Hello World!";
echo strrev($x);
PHP trim() Function
Whitespace is the space before and/or after the actual text, and very often you want to remove this space.
The trim() function removes any whitespace from the beginning or the end.
Example
Remove any whitespace from the beginning or the end:
$x = " Hello World! ";
echo trim($x);
PHP explode() Function
The explode() function splits a string into an array.
The first parameter of the explode() function represents the "separator". The "separator" specifies where to split the string.
Note: The separator is required.
Example
Split the string into an array. Use the space character as separator:
$x = "Hello lovely World!";
$y = explode(" ", $x);
//Use print_r() to display the result
print_r($y);
Complete PHP String Reference
For a complete reference of all string functions, go to the complete PHP String Reference.