PHP Modify Strings

Back to ACP page

Table of Contents

PHP Modify Strings

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

<h2>php Modify Strings</h2> <p>From w3schools.com, Experiment by Teeratus_R </p> <h4>Upper Case</h4> <p>The strtoupper() function returns the string in upper case:</p> <?php $x = "Hello World!"; echo strtoupper($x); ?> <h4>Lower Case</h4> <p>The strtolower() function returns the string in lower case:</p> <?php $x = "Hello World!"; echo strtolower($x); ?> <h4>Replace String</h4> <p>The str_replace() function replaces some characters with some other characters in a string.</p> <?php $x = "Hello World!"; echo str_replace("World", "Dolly", $x); ?> <h4>Reverse a String</h4> <p>The PHP strrev() function reverses a string.</p> <?php $x = "Hello World!"; echo strrev($x); ?> <h4>Remove Whitespace</h4> <p>Whitespace is the space before and/or after the actual text, and very often you want to remove this space.</p> <?php $x = " Hello World! "; echo $x . "<br>"; echo trim($x); ?>

Example 1

Result View Example

PHP Convert String into Array

<h2>php Convert String into Array</h2> <p>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.</p> <?php $x = "Hello World!"; $y = explode(" ", $x); //Use the print_r() function to display the result: print_r($y); /* Result: Array ( [0] => Hello [1] => World! ) */ ?>

Example 2

Result View Example

Document

Document in project

You can Download PDF file.

Reference