0% found this document useful (0 votes)
8 views

PHP - Modify Strings

PHP - Modify Strings

Uploaded by

159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PHP - Modify Strings

PHP - Modify Strings

Uploaded by

159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP - Modify Strings

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

Upper Case
Example
The strtoupper() function returns the string in upper case:
$x = "Hello World!";
echo strtoupper($x);

Lower Case
Example
The strtolower() function returns the string in lower case:
$x = "Hello World!";
echo strtolower($x);

Replace String
The PHP str_replace() function replaces some characters with some other characters in a string.

Example
Replace the text "World" with "Dolly":
$x = "Hello World!";
echo str_replace("World", "Dolly", $x);
Reverse a String
The PHP strrev() function reverses a string.

Example
Reverse the string "Hello World!":
$x = "Hello World!";
echo strrev($x);

Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often you want to remove this
space.

Example
The trim() removes any whitespace from the beginning or the end:
$x = " Hello World! ";
echo trim($x);

Convert String into Array


The PHP explode() function splits a string into an array.

The first parameter of the explode() function represents the "separator". The "separator" specifies
where to split the string.
Note: The separator is required.

Example
Split the string into an array. Use the space character as separator:
$x = "Hello World!";
$y = explode(" ", $x);

//Use the print_r() function to display the result:


print_r($y);

/*
Result:
Array ( [0] => Hello [1] => World! )
*/

https://www.w3schools.com/php/php_string_modify.asp

You might also like