Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Strings Functions

The document discusses various PHP string functions including strtolower(), strtoupper(), ucfirst(), lcfirst(), ucwords(), strrev(), strlen(), strpos(), str_replace(), str_split(), str_word_count(), strcmp(), substr(), strtok(), str_repeat(), and substr_count(). Examples are provided for each function to demonstrate its usage.

Uploaded by

Just
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Strings Functions

The document discusses various PHP string functions including strtolower(), strtoupper(), ucfirst(), lcfirst(), ucwords(), strrev(), strlen(), strpos(), str_replace(), str_split(), str_word_count(), strcmp(), substr(), strtok(), str_repeat(), and substr_count(). Examples are provided for each function to demonstrate its usage.

Uploaded by

Just
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

210301501

Website FCAIT
SEM V
Development BCA
Using PHP &
MySQL

- Dr. Disha Shah


- Dr. Poonam Dang
Module - 3
Strings, Date & Time,
Working with Forms

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

Convert all characters to uppercase:


Syntax:
strtoupper(string)

<?php
echo strtoupper("Hello WORLD!");
?>
ucfirst() Function

Convert the first character of "hello" to uppercase:


Syntax
ucfirst(string)

<?php
echo ucfirst("hello world!");
?>
lcfirst() Function

The lcfirst() function converts the first character of


a string to lowercase.
Syntax
lcfirst(string)

<?php
echo lcfirst("hello world!");
?>
ucwords() Function

Convert the first character of each word to


uppercase.

Syntax
ucwords(string)
<?php
echo ucwords("hello world");
?>
Strrev() Function

The strrev() function reverses a string.


Syntax
strrev(string)

<?php
echo strrev("Hello World!");
?>
strlen() Function

The strlen() function returns the length of a string.


Syntax
strlen(string)

<?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

char Optional. Specifies special characters to be considered as words.


str_word_count() Function
<?php
echo str_word_count("Hello world!");
?>

$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:

0 - if the two strings are equal


<0 - if string1 is less than string2
>0 - if string1 is greater than string2
strcmp() Function
<?php

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

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
substr() Function

length Optional. Specifies the length of the returned


string. Default is to the end of the string.

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
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>";
?>
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>";
?>
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
strtok() Function
<?php
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");

while ($token !== false)


{
echo "$token<br>";
$token = strtok(" ");
}
?>
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
str_repeat() function
<?php
echo str_repeat(".",13);
?>
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

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
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"
?>

You might also like