Skip to content
Related Articles
Open in App
Not now

Related Articles

Explain some string functions of PHP

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 20 Oct, 2021
Improve Article
Save Article
Like Article

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 output
echo strlen($str);
  
// Prints 13 in a new line
echo "<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);
  
?>

Output

!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

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 string
echo $str;
  
// Removes whitespaces from right end
echo chop($str) . "<br>";
  
// Removes whitespaces from both ends
echo trim($str) . "<br>";
  
// Removes whitespaces from right end
echo rtrim($str) . "<br>";
  
// Removes whitespaces from left end
echo ltrim($str);
  
?>

Output

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 
) 

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!