Faculty of Computer Applications &
Information Technology
BCA
210301501 WEBSITE DEVELOPMENT USING
PHP & MySQL
Unit 3
Strings, Date and Time, Working with Forms
Strings
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.
Faculty of Computer Applications & IT
strtolower() Function
Convert all characters to lowercase:
Syntax:
strtolower(string)
<?php
echo strtolower("Hello WORLD.");
?>
Faculty of Computer Applications & IT
strtoupper() Function
Convert all characters to uppercase:
Syntax:
strtoupper(string)
<?php
echo strtoupper("Hello WORLD!");
?>
Faculty of Computer Applications & IT
ucfirst() Function
Convert the first character of "hello" to uppercase:
Syntax
ucfirst(string)
<?php
echo ucfirst("hello world!");
?>
Faculty of Computer Applications & IT
lcfirst() Function
The lcfirst() function converts the first character of a
string to lowercase.
Syntax
lcfirst(string)
<?php
echo lcfirst("hello world!");
?> Faculty of Computer Applications & IT
ucwords() Function
Convert the first character of each word to uppercase.
Syntax
ucwords(string)
<?php
echo ucwords("hello world");
?>
Faculty of Computer Applications & IT
Strrev() Function
The strrev() function reverses a string.
Syntax
strrev(string)
<?php
echo strrev("Hello World!");
?>
Faculty of Computer Applications & IT
strlen() Function
The strlen() function returns the length of a string.
Syntax
strlen(string)
<?php
echo strlen("Hello");
?>
Faculty of Computer Applications & IT
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
Faculty of Computer Applications & IT
strpos() Function
<?php
echo strpos("I love php, I love php too!","php");
?>
Faculty of Computer Applications & IT
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
Faculty of Computer Applications & IT
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;
?>
Faculty of Computer Applications & IT
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
Faculty of Computer Applications & IT
str_split() Function
<?php
print_r(str_split("Hello"));
?>
<?php
$a = str_split("Hello");
print_r($a);
echo $a[0];
?> Faculty of Computer Applications & IT
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 associative array where keys are positions of the words in the string, and values are
the words themselves.
char Optional. Specifies special characters to be considered as words.
Faculty of Computer Applications & IT
str_word_count() Function
<?php
$str = "Hello world, PHP is great!";
echo
< str_word_count($str);
// Output: 5
?>
<?php
$str = "Hello world, PHP is great!";
print_r(str_word_count($str, 1));
/*
Output:
Array
(
[0] => Hello
[1] => world
[2] => PHP
[3] => is
[4] => great
)
*/
?> Faculty of Computer Applications & IT
str_word_count() Function
<?php
$str = "Hello world, PHP is great!";
print_r(str_word_count($str, 2));
/*
Output:
Array
(
[0] => Hello
[6] => world
[13] => PHP
[17] => is
[20] => great
)
Faculty of Computer Applications & IT
*/
str_word_count() Function
<?php
$str = "It's a well-known fact";
print_r(str_word_count($str, 1, "'-"));
/*
Output:
Array
(
[0] => It's
[1] => a
[2] => well-known
[3] => fact
)
*/
Faculty of Computer Applications & IT
?>
str_word_count() Function
<?php
$str = "It's a well-known fact";
print_r(str_word_count($str, 1));
?>
Output
Array
(
[0] => It
[1] => s
[2] => a
[3] => well
[4] => known
[5] => fact Faculty of Computer Applications & IT
str_word_count() Function
<?php
print_r(str_word_count("Hello world & good morning!",1));
?>
Array
[0] => Hello
[1] => world
[2] => good
[3] => morning
Only letters A–Z and a–z are considered word characters. & is ignored and treated as a separator.
Faculty of Computer Applications & IT
str_word_count() Function
<?php
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
Array
[0] => Hello
[1] => world The third parameter "&" tells PHP to treat & as part of a
word.
Now & is considered its own "word" between "world" and
[2] => &
"good".
[3] => good
[4] => morning
) Faculty of Computer Applications & IT
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
Faculty of Computer Applications & IT
strcmp() Function
Return Value: This function returns:
0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2
Faculty of Computer Applications & IT
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 Start can be any of
following:
A positive number - Start at a specified position in the string
A negative number - Start at a specified position from the end of the string
0 - Start at the first character in string
Faculty of Computer Applications & IT
substr() Function
length Optional. Number of characters to extract.
Length can be any of following:
A positive number - The length to be returned from the start parameter
Negative number - The length to be returned from the end of the string
Faculty of Computer Applications & IT
substr() Function
H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
H e l l o W o r l d
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Faculty of Computer Applications & IT
substr() Function
Example: Positive start
substr("Hello World", 6); // "World"
Starts at index 6 → "W" and continues to the end.
Example: Negative start
substr("Hello World", -5); // "World"
Starts 5 characters from the end → "W" and continues to the end.
Faculty of Computer Applications & IT
substr() Function
Example: Positive start + length
substr("Hello World", 0, 5); // "Hello"
Starts at index 0, takes 5 characters.
Example: Negative length
substr("Hello World", 0, -6); // "Hello"
Starts at index 0, stops 6 characters before the end.
Positive numbers → count from left starting at 0.
Negative numbers → count from right starting at -1.
Faculty of Computer Applications & IT
substr() Function
Using the start parameter with different positive and negative numbers:
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
Faculty of Computer Applications & IT
substr() Function
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>
Faculty of Computer Applications & IT
strtok() Function
The strtok() function splits a string into smaller strings (tokens).
Syntax
strtok(string,split)
Parameter Description
string Required. Specifies the string to split
split Required. Specifies one or more split characters
Faculty of Computer Applications & IT
strtok() Function
<?php
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");
while ($token !== false)
echo "$token<br>";
$token = strtok($string, " ");
?>
Faculty of Computer Applications & IT
strtok() Function
<?php
$string = "car,cycle,motor,bus,train";
$token = strtok($string, ","); // first token
while ($token !== false) {
echo "$token<br>";
$token = strtok(","); // next token (no $string here)
?>
Faculty of Computer Applications & IT
str_repeat() function
The str_repeat() function repeats a string a specified number of times.
Syntax:
str_repeat(string,repeat)
Parameter Description
string Required. Specifies the string to repeat
repeat Required. Specifies the number of times the string will be repeated. Must
be greater or equal to 0
Faculty of Computer Applications & IT
str_repeat() function
<?php
echo str_repeat(".",13);
?>
Faculty of Computer Applications & IT
substr_count() Function
The substr_count() function counts the number of times a substring occurs in a string.
The substring is case-sensitive.
This function does not count overlapped substrings.
This function generates a warning if the start parameter plus the length parameter is greater than
the string length
Faculty of Computer Applications & IT
substr_count() Function
Syntax
substr_count(string,substring,start,length)
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
Faculty of Computer Applications & IT
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"
?>
Faculty of Computer Applications & IT