Explain some string functions of PHP - GeeksforGeeks

20 Oct, 2021
In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols as well. For example, “Hello World!”, “ID-34#90” etc. PHP also allows single quotes(‘ ‘) for defining a string. Every programming language provides some in-built functions for the manipulation of strings. Some of the basic string functions provided by PHP are as follows:
strlen() Function: It returns the length of the string i.e. the count of all the characters in the string including whitespaces characters.
Syntax:
strlen(string or variable name)
Example:
PHP
<?php   $str = "Hello World!";  // Prints 12 as outputecho strlen($str);  // Prints 13 in a new lineecho "<br>" . strlen("GeeksForGeeks");   ?>
 
Output:
12
13
strrev() Function: It returns the reversed string of the given string.
Syntax:
strrev(string or variable name)
Example:
PHP
<?php  $str = "Hello World!";echo strrev($str);  ?>
!dlroW olleH
trim(), ltrim(), rtrim(), and chop() Functions: It remove white spaces or other characters from the string.  They have two parameters: one string and another charList, which is a list of characters that need to be omitted.
trim() – Removes characters or whitespaces from both sides.
rtrim() & chop() –  Removes characters or whitespaces from right side.
ltrim() – Removes characters or whitespaces from the left side.
Note: The browser output of the code given in the examples below may vary from HTML output for these functions.
Syntax:
rtrim(string, charList)
ltrim(string, charList)
trim(string, charList)
chop(string, charList)
Parameter Values:
$string: This mandatory parameter specifies the string to be checked.
$charlist: This optional parameter specifies which characters are to be removed from the string. In case, this parameter is not provided, the following characters are removed :“\0” – NULL“\t” – tab“\n” – new line“\x0B” – vertical tab“\r” – carriage return” “ – ordinary white space
“\0” – NULL
“\t” – tab
“\n” – new line
“\x0B” – vertical tab
“\r” – carriage return
” “ – ordinary white space
Note – The parameter charList is available only in PHP version 4.1 or higher.
Example:
PHP
<?php  $str = "\nThis is an example for string functions.\n";  // Prints original stringecho $str;  // Removes whitespaces from right endecho chop($str) . "<br>";  // Removes whitespaces from both endsecho trim($str) . "<br>";  // Removes whitespaces from right endecho rtrim($str) . "<br>";  // Removes whitespaces from left endecho ltrim($str);  ?>
This is an example for string functions.

This is an example for string functions.<br>This is an example for string functions.<br>
This is an example for string functions.<br>This is an example for string functions.

strtoupper() and strtolower() Function: It returns the string after changing cases of its letters.
strtoupper() – It returns the string after converting all the letters to uppercase.
strtolower() – It returns the string after converting all the letters to lowercase.
Syntax:
strtoupper(string)
strtolower(string)
Example:
PHP
<?php  $str = "GeeksForGeeks";echo strtoupper($str)."<br>";echo strtolower($str);  ?>
Output:
GEEKSFORGEEKS
geeksforgeeks
str_split() Function: It returns an array containing parts of the string.
Syntax:
str_split(string, length)
Parameters:
string: It specifies the string to be checked, it can also be a variable name of type string.
length: It specifies the length of each part of the string to be stored in the string, by default, it is 1. If the length is larger than the size of the string, then the complete string is returned.
Example:
PHP
<?php  $str = "GeeksForGeeks";print_r(str_split($str));echo "<br>";print_r(str_split($str, 3));  ?>
Output:
Array ( 
    [0] => G 
    [1] => e 
    [2] => e 
    [3] => k 
    [4] => s 
    [5] => F 
    [6] => o 
    [7] => r 
    [8] => G 
    [9] => e 
    [10] => e 
    [11] => k 
    [12] => s 
)
Array ( 
    [0] => Gee 
    [1] => ksF 
    [2] => orG 
    [3] => eek 
    [4] => s 
) 
PHP-Questions
PHP-string
Picked
TrueGeek-2021
PHP
Strings
TrueGeek
Web Technologies
Strings
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
Comparing two dates in PHP
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types