What is String in PHP
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:
$my_string = 'Hello World';
You can also use double quotation marks ("). However, single
and double quotation marks work in different ways. Strings
enclosed in single-quotes are treated almost literally, whereas
the strings delimited by the double quotes replaces variables
with the string representations of their values as well as
specially interpreting certain escape sequences.
The escape-sequence replacements are:
\n is replaced by the newline character
\r is replaced by the carriage-return 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 (\)
Basic PHP String Functions
In this section, we will discuss a few basic string functions which are most
commonly used in PHP scripts.
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Calculate String Length</title>
</head>
<body>
<?php
$my_str = 'Welcome to Tutorial Republic';
// Calculating and displaying string length
echo strlen($my_str);
?>
</body>
</html>
1. Getting length of a String
PHP has a predefined function to get the length of a string. Strlen()
displays the length of any string. It is more commonly used in validating
input fields where the user is limited to enter a fixed length of characters.
Syntax
Strlen(string);
Example
<?php
echo strlen(“Welcome to Cloudways”);//will return the length of given
string
?>
Output
20
2. Counting of the number of words in a String
Another function which enables display of the number of words in any
specific string is str_word_count(). This function is also useful in
validation of input fields.
Syntax
Str_word_count(string)
Example
<?php
echo str_word_count(“Welcome to Cloudways”);//will return the number
of words in a string
?>
Output
3
3. Reversing a String
Strrev() is used for reversing a string. You can use this function to get the
reverse version of any string.
Syntax
Strev(string)
Example
<?php
echo strrev(“Welcome to Cloudways”);// will return the string starting
from the end
?>
Output
syawduolC ot emocleW
Reverse String Program in PHP
<?php
$string = "hitesh";
$length = strlen($string);
for ($i=($length-1) ; $i >= 0 ; $i--)
{
echo $string[$i];
}
?>
Output
hsetih
4. Finding Text Within a String
Strpos() enables searching particular text within a string. It works simply
by matching the specific text in a string. If found, then it returns the
specific position. If not found at all, then it will return “False”. Strops() is
most commonly used in validating input fields like email.
Syntax
Strpos(string,text);
Example
<?php
echo strpos(“Welcome to Cloudways”,”Cloudways”);
?>
Output
11
5. Replacing text within a string
Str_replace() is a built-in function, basically used for replacing specific
text within a string.
Syntax
Str_replace(string to be replaced,text,string)
Example
<?php
echo str_replace(“cloudways”, “the programming world”, “Welcome to
cloudways”);
?>
Output
Welcome to the programming world
6. Converting lowercase into Title Case
Ucwords() is used to convert first alphabet of every word into uppercase.
Syntax
Ucwords(string)
Example
<?php
echo ucwords(“welcome to the php world”);
?>
Output
Welcome To The Php World
7. Converting a whole string into UPPERCASE
Strtoupper() is used to convert a whole string into uppercase.
Syntax
Strtoupper(string);
Example
<?php
echo strtoupper(“welcome to cloudways”);// It will convert all letters of
string into uppercase
?>
Output
WELCOME TO CLOUDWAYS
8. Converting whole String to lowercase
Strtolower() is used to convert a string into lowercase.
Syntax
Strtolower(string)
Example
<?php
echo strtolower(“WELCOME TO CLOUDWAYS”);
?>
Output
welcome to cloudways
9. Repeating a String
PHP provides a built-in function for repeating a string a specific number of
times.
Syntax
Str_repeat(string,repeat)
Example
<?php
echo str_repeat(“=”,13);
?>
Output
=============
10. Comparing Strings
You can compare two strings by using strcmp(). It returns output either
greater than zero, less than zero or equal to zero. If string 1 is greater than
string 2 then it returns greater than zero. If string 1 is less than string 2 then
it returns less than zero. It returns zero, if the strings are equal.
Syntax
Strcmp(string1,string2)
Example
<?php
echo strcmp(“Cloudways”,”CLOUDWAYS”);
echo “<br>”;
echo strcmp(“cloudways”,”cloudways”);//Both the strings are equal
echo “<br>”;
echo strcmp(“Cloudways”,”Hosting”);
echo “<br>”;
echo strcmp(“a”,”b”);//compares alphabetically
echo “<br>”;
echo strcmp(“abb baa”,”abb baa caa”);//compares both strings and
returns the result in terms of number of characters.
?>
Output
1
-1
-1
-4
11. Displaying part of String
Through substr() function you can display or extract a string from a
particular position.
Syntax
substr(string,start,length)
Example
<?php
echo substr(“Welcome to Cloudways”,6).”<br>”;
echo substr(“Welcome to Cloudways”,0,10).”<br>”;
?>
Output
e to Cloudways
Welcome to
12. Removing white spaces from a String
Trim() is dedicated to remove white spaces and predefined characters from
a both the sides of a string.
Syntax
trim(string,charlist)
Example
<?php
$str = “Wordpess Hosting”;
echo $str . “<br>”;
echo trim(“$str”,”Wording”);
?>
Output
Wordpess Hosting
pess Host