PHP – Modify Strings

PHP offers a variety of built-in functions to modify strings.

Tutorials dojo strip



Convert to Upper Case

The strtoupper() function converts a string to upper case.

Example:

$brand = "Yamaha is great!";
echo strtoupper($brand);




Convert to Lower Case

The strtolower() function converts a string to lower case.

Example:

$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”:

$car = "I love Ferrari!";
echo str_replace("Ferrari", "Lamborghini", $car);




Reverse a String

The strrev() function reverses the characters in a string.

Example:

$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:

$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:

$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! )
*/
Tutorials dojo strip
Scroll to Top