PHP strtoupper() Function
PHP's built-in string functions can also change your text, and the next few sections show the most useful ones.
First is strtoupper(), which gives you back the string written entirely in capital letters.
Example
Every letter of $x becomes a capital:
Output: PHP script
<?php
$x = "Hello World!";
echo strtoupper($x);
?>
PHP strtolower() Function
strtolower() does the opposite, giving you the string with only small letters.
Example
Every letter of $x becomes small:
Output: PHP script
<?php
$x = "Hello World!";
echo strtolower($x);
?>
PHP str_replace() Function
Use str_replace() to swap some characters in a string for other characters.
Example
The word "World" is swapped for "Dolly":
Output: PHP script
<?php
$x = "Hello World!";
echo str_replace("World", "Dolly", $x);
?>
PHP strrev() Function
strrev() flips a string, so its last character comes first.
Example
"Hello World!" printed backwards:
Output: PHP script
<?php
$x = "Hello World!";
echo strrev($x);
?>
PHP trim() Function
Text often arrives with extra spaces before or after the part you care about. That surrounding space is called whitespace, and you usually want it gone.
The trim() function strips it from both ends of a string.
Example
The spaces on each side of $x disappear from the output:
Output: PHP script
<?php
$x = " Hello World! ";
echo trim($x);
?>
PHP explode() Function
To break a string into pieces and get those pieces as an array, call explode().
Its first parameter is the "separator", which tells the function where each cut should be made.
Note: You can't leave the separator out; it is required.
Example
Cutting at every space gives an array with three elements:
Output: PHP script
<?php
$x = "Hello lovely World!";
$y = explode(" ", $x);
//Use print_r() to display the result
print_r($y);
?>