Strings in PHP
Strings in PHP
Function Purpose
Addcslashes addcslashes function will add backslash
Syntax : string addcslashes (string, charlist); before the specified characters
Parameter : in charlist parameter for the string.
string – This is
a Required parameter. which needs to Example:
be escaped. echo addcslashes("Hi from Subodh ","o");
charlist – This is Output
a Required parameter. It can have one “Hi fr\om Sub\odh “.
characters or collection of characters. echo addcslashes($strExample,'A..Z')."<br>";
echo addcslashes($strExample,'a..z')."<br>";
\Hi from \Subodh
H\i \f\r\o\m S\u\b\o\d\h
1
charlist – Optional. This is optional is an alias of- rtrim() function.
parameter, If any value is provided, then example
specified characters will be removed from echo chop(“hello “);
right of a string. output
The following characters are removed if the hello
charlist parameter is empty: echo chop(“hello”,”0”);
“\0” – NULL output
“\t” – tab hell
“\n” – new line
“\r” – carriage return
” ” – ordinary white space
“\x0B” – vertical tab
2
specified characters will be removed from output
left of a string. hello
The following characters are removed if the echo ltrim(“hello”,”h”);
charlist parameter is empty: output
“\0” – NULL ell0
“\t” – tab
“\n” – new line
“\r” – carriage return
” ” – ordinary white space
“\x0B” – vertical tab
3
str_pad str_pad() function will pad a string to a certain
str_pad (string, length, pad_string, length with another string.
pad_type ); Example
Parameter : $strExample = "Hi from Subodh.";
string – This is a Required parameter. It echo str_pad($strExample,23,"!");
is the input string on which function will output
be performed. Hi from Subodh.!!!!!!!!
length – This is a Required parameter. It
sets value of new length of a input string
parameter. if the value of pad_length is
negative, less than, or equal to the length
of the input string, no padding takes
place, and input will be returned.
pad_string – This is
a Required parameter. It sets string to
be padded with input string. Default
padded string is whitespace.
pad_type – This is
an Optional parameter. It can
be Specifies what side to pad the string.
STR_PAD_RIGHT – Padding is done
to the right side of the string. This
is default value.
STR_PAD_LEFT – Padding is done
to the left side of the string
STR_PAD_BOTH – Padding is done
to both sides of the string. If not an
even number, the right side gets the
extra padding.
4
str_split() str_split() function will breaks a string into an array.
str_split ( string, length );
string – This is a Required parameter. It is the Example
input string on which function will be $strExample = "Hi from Subodh";
performed. print_r(str_split($strExample));
length – This is an Optional parameter. It sets
the length of each array element. Default Array ( [0] => H [1] => i [2] => [3] => f [4] => r [5] => o
length value is 1. [6] => m [7] => [8] => S [9] => u [10] => b [11] => o
Output : [12] => d [13] =>h)
5
the first string which will be compared. Example
string2 – This is a Required parameter. It is echo strcasecmp("Hi from Subodh.","hI FROM
the second string which will be compared. SUBODH.");
strchr()
strchr ( string, search, before_search ); strchr() function will find the first occurrence of
string – This is a Required parameter. This is search in a string. This function is an alias of the
the string which is to be searched. strstr() function.
search – This is a Required parameter. It
contains the value to be seacrhed. If we pass
this parameter as a number, then it will search Example:
for the character matching the ASCII value of
the number. echo strchr("Hi from Subodh.","from");
before_search – This is // case-sensitive comparison
an Optional parameter. A Boolean value,
default is set to “false”. If set to “true”, it
returns the part of the string before the first Output
occurrence of the search parameter. from Subodh.
Output :
echo strchr("Hi from Subodh.",102);// ASCII value of f
It will returns below values: is 102.
6
Example:
$strExample = "An EXAMPLE of a strtolower()
function.";
echo strtolower($strExample);
output
an example of a strtolower() function.
strtoupper() Strtolower() function will take string as input and
converts all letter to uppercase letter, if that character
strtoupper ( string ); is alphabetic.
Example:
$strexample = "an example of a strtoupper()
function.";
echo strtoupper($strexample);
Output
AN EXAMPLE OF A STRTOLOWER() FUNCTION.
strops() - The strpos() function finds the Example
position of the first occurrence of a string echo strrpos("I love php, I love php too!","php");
inside another string.(case sensitive) output
19
strrpos() - Finds the position of the last
occurrence of a string inside another string echo strpos("I love php, I love php too!","php");
(case-sensitive) output
stripos() - Finds the position of the first 7
occurrence of a string inside another string
(case-insensitive)
strripos() - Finds the position of the last
occurrence of a string inside another string
(case-insensitive)
Parameter
Return value:
Position or FALSE
7
8