PHP offers a variety of built-in functions to modify strings.
Convert to Upper Case
The strtoupper()
function converts a string to upper case.
Example:
PHP
x
$brand = "Yamaha is great!";
echo strtoupper($brand);
Convert to Lower Case
The strtolower()
function converts a string to lower case.
Example:
PHP
$brand = "Ducati is amazing!";
echo strtolower($brand);
Replace String Content
The str_replace()
function replaces specific characters in a string with other characters.
Example:
Replace “Ferrari” with “Lamborghini”:
PHP
$car = "I love Ferrari!";
echo str_replace("Ferrari", "Lamborghini", $car);
Reverse a String
The strrev()
function reverses the characters in a string.
Example:
PHP
$brand = "Harley Davidson!";
echo strrev($brand);
Remove Whitespace
Whitespace refers to any spaces before and after the actual text. The trim()
function removes these spaces.
Example:
PHP
$greeting = " Hello Riders! ";
echo trim($greeting);
Convert String to Array
The explode()
function splits a string into an array based on a specified separator.
Example:
Split the string into an array using space as a separator:
PHP
$text = "Ride safe always!";
$words = explode(" ", $text);
// Use the print_r() function to display the result:
print_r($words);
/*
Result:
Array ( [0] => Ride [1] => safe [2] => always! )
*/