Modify Strings

PHP has a set of built-in functions that you can use to modify strings.


  • strtolower() and strtoupper(): Converts a string to lowercase or uppercase.


Example
<!DOCTYPE html>
<html>
<body>

<?php

echo strtolower("Hello World!"); // Outputs: hello world!
echo strtoupper("Hello World!"); // Outputs: HELLO WORLD!


?>

</body>
</html>

Try it yourself

  • str_replace(): Replaces all occurrences of a search string with a replacement string.
Example
<!DOCTYPE html>
<html>
<body>

<?php

$newString = str_replace("World", "PHP", "Hello World"); // Outputs: Hello PHP

echo $newString;

?>

</body>
</html>

Try it yourself

  • strrev(): This function takes a string as input and returns it reversed.
Example
<!DOCTYPE html>
<html>
<body>

<?php
$originalString = "Hello, World!";
$reversedString = strrev($originalString);

echo $reversedString; // Outputs: !dlroW ,olleH
?>

</body>
</html>

Try it yourself

  • trim(): This function is used to remove whitespace and other predefined characters from the beginning and end of a string.
Example
<!DOCTYPE html>
<html>
<body>

<?php

$text = ",.;Hello, World!;,.";
$trimmedText = trim($text, ",.;");

echo $trimmedText; // Outputs: "Hello, World!"

?>


</body>
</html>

Try it yourself

  • explode() : This function is used to split a string into an array based on a specified delimiter.
Example
<!DOCTYPE html>
<html>
<body>

<?php

$string = "apple,banana,orange";
$array = explode(",", $string);

print_r($array);
// Outputs: Array ( [0] => apple [1] => banana [2] => orange )


?>


</body>
</html>

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.