PHP Basic Tutorial
PHP has a set of built-in functions that you can use to modify strings.
Method |
Description |
Example |
---|---|---|
strtolower() and strtoupper() |
Converts a string to lowercase or uppercase |
echo strtolower("Hello World!"); // Outputs: hello world! echo strtoupper("Hello World!"); // Outputs: HELLO WORLD! |
str_replace() |
Replaces all occurrences of a search string with a replacement string |
$newString = str_replace("World", "PHP", "Hello World"); // Outputs: Hello PHP echo $newString; |
strrev() |
This function takes a string as input and returns it reversed |
$originalString = "Hello, World!"; $reversedString = strrev($originalString); echo $reversedString; // Outputs: !dlroW ,olleH |
trim() |
This function is used to remove whitespace and other predefined characters from the beginning and end of a string |
$text = ",.;Hello, World!;,."; $trimmedText = trim($text, ",.;"); echo $trimmedText; // Outputs: "Hello, World!" |
explode() |
This function is used to split a string into an array based on a specified delimiter |
$string = "apple,banana,orange"; $array = explode(",", $string); print_r($array); // Outputs: Array ( [0] => apple [1] => banana [2] => orange ) |