Strings Functions
Strings Functions
Website FCAIT
SEM V
Development BCA
Using PHP &
MySQL
2
Index
●
Strings
●
Date & Time
●
Working with Forms
3
String
●
PHP string is a sequence of characters
i.e., used to store and manipulate
text.
●
PHP supports only 256-character set
and so that it does not offer native
Unicode support.
4
String Functions
ToLower() ●
Strreplace()
ToUpper() ●
Strsplit()
Ucfirst() ●
Strwordcount()
lclast() ●
Strcmp()
UcWords()
●
Substr()
Strrev()
●
Strtok()
●
str_repeat()
Strlen()
●
substr_count()
Strpos()
. (dot) operator
5
strtolower() Function
Convert all characters to lowercase:
Syntax:
strtolower(string)
<?php
echo strtolower("Hello WORLD.");
?>
6
strtoupper() Function
<?php
echo strtoupper("Hello WORLD!");
?>
ucfirst() Function
<?php
echo ucfirst("hello world!");
?>
lcfirst() Function
<?php
echo lcfirst("hello world!");
?>
ucwords() Function
Syntax
ucwords(string)
<?php
echo ucwords("hello world");
?>
Strrev() Function
<?php
echo strrev("Hello World!");
?>
strlen() Function
<?php
echo strlen("Hello");
?>
strpos() Function
The strpos() function finds the position of the first
occurrence of a string inside another string.
The strpos() function is case-sensitive.
Syntax
strpos(string,find,start)
Parameter Description
string Required. Specifies the string to search
find Required. Specifies the string to find
start Optional. Specifies where to begin the search
strpos() Function
<?php
echo strpos("I love php, I love php
too!","php");
?>
str_replace() Function
The str_replace() function replaces some characters with some other
characters in a string.
This function is case-sensitive.
Syntax
str_replace(find,replace,string,count)
Parameter Description
find Required. Specifies the value to find
replace Required. Specifies the value to replace the value in find
string Required. Specifies the string to be searched
count Optional. A variable that counts the number of
replacements
str_replace() Function
<?php
$oldstr = "The cat was black";
$newstr = str_replace("black", "white",
$oldstr, $a);
// Displays: The cat was white
echo $newstr;
echo $a;
?>
str_split() Function
The str_split() function splits a string into an array.
Syntax
str_split(string,length)
Parameter Description
string Required. Specifies the string to split
length Optional. Specifies the length of each
array element. Default is 1
<?php
print_r(str_split("Hello"));
?>
<?php
$a = str_split("Hello");
print_r($a);
echo $a[0];
?>
str_word_count() Function
The str_word_count() function counts the number of words in a string.
Syntax
str_word_count(string,return,char)
Parameter Description
string Required. Specifies the string to check
return Optional. Specifies the return value of the str_word_count()
function.
Possible values:
0 - Default. Returns the number of words found
1 - Returns an array with the words from the string
2 - Returns an array where the key is the position of the word in the
string, and value is the actual word
$a = str_word_count("Hello world!",1);
echo $a[1];
str_word_count() Function
<?php
print_r(str_word_count("Hello world!",2));
?>
<?php
print_r(str_word_count("Hello world & good
morning!",1));
print_r(str_word_count("Hello world & good
morning!",1,"&"));
?>
strcmp() Function
The strcmp() function compares two strings.
Syntax
strcmp(string1,string2)
Parameter Description
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to
compare
strcmp() Function
Return Value: This function returns:
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?>
substr() Function
The substr() function returns a part of a string.
Syntax
substr(string,start,length)
Parameter Description
string Required. Specifies the string to return a part of
start Required. Specifies where to start in the string
Parameter Description
string Required. Specifies the string to split
split Required. Specifies one or more split
characters
strtok() Function
<?php
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");
Parameter Description
string Required. Specifies the string to check
substring Required. Specifies the string to search for
start Optional. Specifies where in string to start
searching
length Optional. Specifies the length of the search
substr_count() Function
<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is"
occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced
to "is is nice"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced
to "s is nice"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced
to "s i"
?>