Modify Strings

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!


Try it yourself

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; 


Try it yourself

strrev()

This function takes a string as input and returns it reversed

$originalString = "Hello, World!";
$reversedString = strrev($originalString);
echo $reversedString; // Outputs: !dlroW ,olleH


Try it yourself

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!"


Try it yourself

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 )


Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.