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
?>
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'
?>
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.
lenght: Optional parameter and specifies the length of the returned 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
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
?>
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
?>
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
?>
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
?>
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
?>
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:
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'
?>
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:
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'
?>
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'
?>
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:
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.
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
)
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
*/
?>
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'
?>
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 '
?>
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:
Syntax: strpos(string, substring, offset)
Example:
<?php
$str = 'Hello World';
echo strpos($str, 'll'); // returns 2
?>
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
?>
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
?>
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
?>
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.
In this article you will lear about most frequently used PHP array functions with the help of beautifully explained examples.
This tutorial will explain you How to implement Singleton Design Pattern in PHP with the help of comprehensive examples.