Frequently used PHP String Functions Explained

Last Updated: 21 Nov, 2023

strlen()

This function is used to get the length of the given string. It accept single parameter and returns length of the string on success, and 0 if the string is empty.

Syntax: strlen(string)

Example:

<?php
$str = 'Hello guys';
echo strlen($str); // returns 10

$str = 'ABCD1234XYZ';
echo strlen($str); // returns 11
?>

strrev()

This function is used to reverse a given string. This function accepts single parameter.

Parameters:
string: Required parameter that specifies the string to be reversed

Syntax: strrev(string)

Example:

<?php
$str = "Hello friends";
echo strrev($str); // returns 'sdneirf olleH'
?>

substr()

This function is used to return the portion of the string specified by the offset and length parameters. It accept 3 parameters (string, start and length) and returns the string accordinglly.

Parameters:

string: Required parameter and specifies the string to return a part of.

start: Required parameter and specifies the start position in the string.

  • A positive number - Start at a specified position in the string
  • A negative number - Start at a specified position from the end of the string
  • 0 - Start at the first character in the string

lenght: Optional parameter and specifies the length of the returned string.

  • A positive number - The length to be returned from the start parameter position
  • A negative number - The lenght to be returned from the end of the string
  • 0, NULL or FALSE - Returns an empty string
Syntax: substr(string, start, lenght)

Example:

<?php
$str = 'Hello World';
echo substr($str, 1) . "<br>";
echo substr($str, 3, 2) . "<br>";
echo substr($str, 5, 3) . "<br>";
echo substr($str, -1, 2) . "<br>";
echo substr($str, 0, 5) . "<br>";
echo substr($str, -3, 3) . "<br>";
?>

Output:

ello World
lo
 Wo
d
Hello
rld

strtolower()

This function is used to convert the given string to the lowercase letter. It accept single parameter and.

Syntax: strtolower(string)

Example:

<?php
$str = 'Hello World';
echo strtolower($str); // returns hello world
?>

strtoupper()

This function is used to convert a given string to uppercase letter. It accepts single parameter.

Syntax: strtoupper(string)

Example:

<?php
$str = 'Hello World';
echo strtoupper($str); // returns HELLO WORLD
?>

ucfirst()

This function is used to convert the first character of a given string to uppercase letter. It accepts single parameter.

Syntax: ucfirst(string)

Example:

<?php
$str = 'hello World';
echo ucfirst($str); // returns Hello World
?>

lcfirst()

This function is used to convert the first character of a given string to lowercase letter. It accepts single parameter.

Syntax: lcfirst(string)

Example:

<?php
$str = 'Hello World';
echo lcfirst($str); // returns hello World
?>

ucwords()

This function is used to convert the first character of each word to uppercase letter. It accepts 2 parameters.

Parameters:
string: Required parameter and speifies the string to be converted
delimeters: Optional parameter and specifies the word separator character

Syntax: ucwords(string, delimeters)

Example:

<?php
$str = 'hello world';
echo ucwords($str, ' '); // returns Hello World
?>

trim()

This function is used to remove whitespace and any other predefined characters from the beginning and end of the string. This function accepts 2 parameters.

Parameters:
string: Required parameter that specifies the string to be checked
characters: Optional parameter that specifies which to remove from the string. If omitted, trim() will remove following characters:

  • "" - ordinary white space
  • "\t" - tab
  • "\n" - new line
  • "\r" - carriage return
  • "\v" - vertical tab
  • "\0" - NULL
Syntax: trim(string, characters)

Example:

<?php
$str = "This is a string ";
echo $str; // returns 'This is a string '
echo trim($str); // returns 'This is a string'
echo trim($str, 'ing '); // returns 'This is a str'
?>

ltrim()

This function is used to remove whitespace and any other predefined characters from the beginning of the string. This function accepts 2 parameters.

Parameters:
string: Required parameter that specifies the string to be checked
characters: Optional parameter that specifies which to remove from the string. If omitted, ltrim() will remove following characters:

  • "" - ordinary white space
  • "\t" - tab
  • "\n" - new line
  • "\r" - carriage return
  • "\v" - vertical tab
  • "\0" - NULL
Syntax: ltrim(string, characters)

Example:

<?php
$str = " This is a string";
echo ltrim($str); // returns 'This is a string'
echo ltrim($str, ' Th'); // returns 'is is a string'
?>

rtrim()

This function is used to remove whitespace and any other predefined characters from the end of the string. This function accepts 2 parameters.

Parameters:
string: Required parameter that specifies the string to be checked
characters: Optional parameter that specifies which to remove from the string. If omitted, rtrim() will remove following characters:
"" - ordinary white space
"\t" - tab
"\n" - new line
"\r" - carriage return
"\v" - vertical tab
"\0" - NULL

Syntax: rtrim(string, characters)

Example:

<?php
$str = " This is a string ";
echo rtrim($str); // returns ' This is a string'
echo rtrim($str, 'string '); // returns ' This is a'
?>

str_replace()

This function is used to replace all occurrences of the search string with the replacement string in a string. This function is case-sensitive, binary-safe and accepts 4 parameters. Please have a look on below given rules of this function:

  • If the string to be searched is an array, it returns an array
  • If the string to be searched is an array, it find and replace is performed on each array element
  • If both find and replace ara arrays, and replace has fewer elements than find, an empty string will be used as replace
  • If find is an array and replace is a string, the replace string will be used for every find value

Parameters:
search: Required parameter that specifies the value to search
replace: Required parameter that specifies the value to replace
subject: Required parameter that specifies the string/array to be searched
count: Optional parameter that counts the number of replacements

Syntax: str_replace(search, replace, subject, count)

Example:

<?php
$str = "Hello friends, how are you?";
echo str_replace('how', 'HOW', $str); // returns 'Hello friends, HOW are you?'

$arr = ['Ram', 'Shyam', 'Mohan', 'Girdhari', 'Gopal', 'Mohan'];
echo '\n';
print_r(str_replace('Mohan', 'Narayan', $arr, $count));
echo '\nReplacements: '.$count;

/* Output:
Array
(
    [0] => Ram
    [1] => Shyam
    [2] => Narayan
    [3] => Girdhari
    [4] => Gopal
    [5] => Narayan
)
Replacements: 2
*/
?>

Note: The str_replace() function is case-sensitive. If you want to ignore the case, use str_ireplace() instead.

str_split()

This function is used to split a string into an array. It accepts 2 parameters.

Parameters:
string: Required parameter that specifies the string to split
length: Optional parameter that specifies the length of each array element. Default value is 1. If less than 1, it will return FALSE; If larger than the length of string, the entire string will be returned as the only element of the array.

Syntax: str_split(string, length)

Example:

<?php
$str = "HiFriends";
print_r(str_split($str));
print_r(str_split($str, 3));
?>

Output:

Array
(
    [0] => H
    [1] => i
    [2] => F
    [3] => r
    [4] => i
    [5] => e
    [6] => n
    [7] => d
    [8] => s
)
Array
(
    [0] => HiF
    [1] => rie
    [2] => nds
)

str_repeat()

This function is used to repeat a string as many time as you specify. It accepts 2 parameters.

Parameters:
string: Required parameter that specifies the string to repeat
times: Required parameter that specifies the number of times the string to be repeated. It must be greater or equal to 0.

Syntax: str_repeat(string, times)

Example:

<?php
$str = "Hello ";
print_r(str_repeat($str, 10));

/*
Output:
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
*/
?>

str_shuffle()

This function is used to randomly shuffle all the characters of a string. It accepts single parameter.

Parameters:
string: Required parameter that specifies the string to shuffle

Syntax: str_shuffle(string)

Example:

<?php
$str = "HelloFriends";
print_r(str_shuffle($str)); // returns like 'osirdFeenllH'
?>

strstr()

This function is used to search for the first occurence of a string inside another string. This function is case-sensitive, binary-safe and accepts 3 parameters.

Parameters:
string: Required parameter that specifies the string to search in
substring: Required parameter that specifies the string to search for
before_search: Optional boolean value, default is "false". If true, returns the part of the string before the first occurrence of the substring parameter.

Syntax: strstr(string, substring, before_search)

Example:

<?php
$str = 'Hello friends, I am your friend.';
echo strstr($str, 'friend'); // returns 'friends, I am your friend.'
echo strstr($str, 'friend', true); // returns 'Hello '
?>

strpos()

This function is used to find the position of the first occurence of a substring inside another string. This function is case-sensitive, binary-safe and accepts 3 parameters.

Parameters:

  • string: Required parameter that specifies the string to search in
  • substring: Required parameter that specifies the string to search for
  • offset: Optional parameter that specifies the search start position. If it is a negative number, it counts from the end of the string.
Syntax: strpos(string, substring, offset)

Example:

<?php
$str = 'Hello World';
echo strpos($str, 'll'); // returns 2
?>

stripos()

This function is used to find the position of the first occurence of a substring inside another string. This function is case-insensitive, binary-safe and accepts 3 parameters.

Parameters:
string: Required parameter that specifies the string to search in
substring: Required parameter that specifies the string to search for
offset: Optional parameter that specifies the search start position

Syntax: stripos(string, substring, offset)

Example:

<?php
$str = 'Hello World';
echo stripos($str, 'w'); // returns 6
?>

strrpos()

This function is used to find the position of the last occurence of a substring inside another string. This function is case-sensitive, binary-safe and accepts 3 parameters.

Parameters:
string: Required parameter that specifies the string to search in
substring: Required parameter that specifies the string to search for
offset: Optional parameter that specifies the search start position. If it is a negative number, it counts from the end of the string.

Syntax: strrpos(string, substring, offset)

Example:

<?php
$str = 'Hello World';
echo strrpos($str, 'o'); // returns 7
?>

strripos()

This function is used to find the position of the lst occurence of a substring inside another string. This function is case-insensitive, binary-safe and accepts 3 parameters.

Parameters:
string: Required parameter that specifies the string to search in
substring: Required parameter that specifies the string to search for
offset: Optional parameter that specifies the search start position

Syntax: strripos(string, substring, offset)

Example:

<?php
$str = 'Hello World';
echo strripos($str, 'L'); // returns 9
?>

 

 

Thank You, Please Share.

Recommended Posts

PHP 7 New Features and Enhancements Explained

PHP 7 New Features and Enhancements Explained

In this easy and simplified tutorial, we are going to learn about most awaited PHP version (PHP 7) and its super important features and enhancements.

Frequently used PHP Array Functions Explained

Frequently used PHP Array Functions Explained

In this article you will lear about most frequently used PHP array functions with the help of beautifully explained examples.

Implement Singleton Design Pattern in PHP

Implement Singleton Design Pattern in PHP

This tutorial will explain you How to implement Singleton Design Pattern in PHP with the help of comprehensive examples.