0% found this document useful (0 votes)
9 views

Strings in PHP

Uploaded by

savixo2871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Strings in PHP

Uploaded by

savixo2871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Strings in PHP

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A


nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The
construct is ideal for embedding PHP code or other large blocks of text without the need for
escaping.
In other words:
$foo = 'bar';
$here = <<<HERE
I'm here , $foo !
HERE;
$now = <<<'NOW'
I'm now , $foo !
NOW;
$here is "I'm here , bar !", while $now is "I'm now , $foo !".

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

Addslashes addslashes function will add backslash ( \ )


Syntax : string addslashes(string); before the predefined characters.
The predefined characters are:
 single quote (‘)
 double quote (“)
 backslash (\)
 NULL
Example
echo addslashes('Hi "from" Subodh.');
output
Hi \”from\” Subodh
bin2hex bin2hex function will converts a string of
string bin2hex (string); ASCII characters to hexadecimal values. The
string can be converted back using the pack()
function.
chop or rtrim chop function will remove predefined
string chop (string, charlist); characters or specified characters in charlist if
string – String to check. any from the right end of a string. This function

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

chr returns a character from the specified ASCII


string chr (ascii); value.
The ASCII value can be specified in
 Decimal : Decimal values are defined by a
leading 0x.
 Octal : Octal values are defined by a leading
0 (Zero)
 Hex : hex values are defined by a leading
0x.
Example
echo chr(65)
echo chr(050)
echo chr(0x50)
output
A
(
P
chunk_split returns smaller chunks(pieces) of a string in
string chunk_split(string, chunklength, chunklength parameter.
end) ; Example
parameter echo chunk_split("Hi All",1,"..");
string – String to be chunked. output
chunklength – This is Optional parameter, H..i.. ..A..l..l..
String will be chunked in chunklength size.
Chunk Default size is 76.
end – This is Optional parameter. End will
defines what to be placed at the end of each
chunk. Default is \r\n

ltrim ltrim function will remove predefined


string ltrim (string, charlist); characters or specified characters in charlist if
string – String to check. any from the left end of a string.
charlist – Optional. This is optional Example
parameter, If any value is provided, then echo ltrim(“ hello“);

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

join join() function will glue(join) array elements in


join ( separator, array ); a string.
Parameter : join() is an alias of the PHP implode() function.
 separator – Optional. This is an optional
parameter. By default it is an empty
string (“”).
 array – Collection of elements to join a
string

str_ireplace str_ireplace() function will replace all


str_ireplace ( find, replace, string, count ); occurrences of the search string with the
str_replace replacement string. It is case – insensitive
str_replace ( find, replace, string, count ); function.
str_replace() function will replace all
Parameter : occurrences of the search string with the
 find – This is a Required parameter. It replacement string. It is case – sensitive
sets values to find. function.
 replace – This is
a Required parameter. It sets values to This function works by the following rules:
replace.  If the string to be searched is an array, it
 string – This is a Required parameter. It returns an array
sets string to search.  If the string to be searched is an array, find
 count – This is an Optional parameter. If and replace is performed with every array
passed, this will be set to the number of element
replacements performed.  If both find and replace are 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
Example
$strExample = "Hi from subodh!";
echo str_ireplace("Hi","Hello",$strExample);
output
Hello from subodh!

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.

str_repeat str_repeat() function repeats a string


str_repeat (string, repeat ); a specified number of times..
Parameter : Example
 string – This is a Required parameter. It echo str_repeat("!",10);
is the input string which will be
repeated. output
 repeat – This is a Required parameter. It !!!!!!!!!!
will set how many times a input string
will be repeated. It should be greater
than or equal to 0. If its value is set to 0,
the function will return an empty string.

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)

This will return an array of string. it also depends


on the length parameter value if,

 length < 1, It will return FALSE.


 length > larger than the length of string, the
entire string will be returned as the only
element of the array.

str_word_count() str_word_count() function will counts the number of


str_word_count (string, returnformat, characterlist words in a string.
);
 string – This is a Required parameter. It is the Example
input string on which function will be $strExample = "Hi from Subodh";
performed. echo str_word_count($strExample)
 returnformat – This is a Optional parameter. It output
tells us about the return value of the 3
str_word_count() function. Below are the
values which can be pass in this $strExample = "Hi from Subodh";
parameter. 0 is the default value. echo str_word_count($strExample,1)
 0 – It is the default value. This will return output
number of words count in a string. Array ( [0] => Hi [1] => from [2] => Subodh)
 1 – This will return an array with the $strExample = "Hi from Subodh";
words from the string. echo str_word_count($strExample,2)
 2 – returns an associative array, where output
the key is the numeric position of the
word inside the string and the value is Array ( [0] => Hi [3] => from [8] => Subodh )
the actual word itself
 characterlist – This is an Optional parameter.
We can set special characters, which will be
consider in word count.
Output :

This will return a number or an array, depending


upon the returnformat parameter value.

strcasecmp() strcasecmp() function will compare two strings. (case


strcasecmp ( string1, string2 ); insensitive match)
 string1 – This is a Required parameter. It is

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.");

Return values in this function are: output:


0
 0 – if the two strings are equal.
 < 0 – if string1 is less than string2.
 > 0 – if string1 is greater than string2.

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.

 the portion of string after the first occurrence Output


is found in the string from Subodh.
 FALSE if the search is not found in the string.
echo "Using before_search 3rd parameter in the
function<br />";
echo strchr("Hi from
tutorialmines.","tutorialmines",true); // case-sensitive
comparison
echo "<br/>";
echo strchr("Hi from tutorialmines.",116,true);// ASCII
value of t is 116.
Using before_search 3rd parameter in the function
Hi from
Hi from

strtolower() strtolower() function will take string as input and


converts all letter to lowercase letter, if that character
strtolower ( string ); is alphabetic.

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

String: Required. Specifies the string to


search

Find: Required. Specifies the string to


find

start : Optional. Specifies where to


begin the search

Return value:

Position or FALSE

7
8

You might also like