String
ETL LABS PVT LTD – PHP 75
What is String
A string is a sequence of letters, numbers,
special characters and arithmetic values or
combination of all. The simplest way to create
a string is to enclose the string literal (i.e. string
characters) in single quotation marks ('), like
this: 1
$my_string = 'Hello World';
You can also use double quotation marks (").
However, single and double quotation marks
work in different ways.
ETL LABS PVT LTD – PHP 76
The escape-sequence
replacements are
• \n is replaced by the newline character
• \r is replaced by the carriage-return
2 character
• \t is replaced by the tab character
• \$ is replaced by the dollar sign itself
($)
• \" is replaced by a single double-quote
(")
• \\ is replaced by a single backslash (\)
ETL LABS PVT LTD – PHP 77
Get The Length of a String
The strlen() function is used to calculate
the number of characters inside a string. It
3
also includes the blank spaces inside the
string.
ETL LABS PVT LTD – PHP 78
Count The Number of
Words in a String
The str_word_count() function
4 counts the number of words in a
string.
ETL LABS PVT LTD – PHP 79
Reverse a String
The strrev() function reverses a string.
ETL LABS PVT LTD – PHP 80
Search For a Specific
Text Within a String
The strpos() function searches for a
specific text within a string.
6
If a match is found, the function returns
the character position of the first
match. If no match is found, it will
return FALSE.
ETL LABS PVT LTD – PHP 81
Replace Text Within a
String
The str_replace() function replaces
some characters with some other characters 7
in a string.
In the example replaces the text "world" with
"Dolly―.
ETL LABS PVT LTD – PHP 82