You are on page 1of 19

Shriram Institute of Information Technology, Paniv

String and Working with Forms

String –
String is a combination of characters, numbers and symbols. String is one of the data type that
PHP support. Everything inside quotes like single quote (‘ ’) or double quote(“ ”) in PHP is a treated as string.
 Formatting string for presentation -
PHP support for formatting string it include following functions –
1) sprintf() function
2) printf() function
3) sscanf() function
1) sprintf() function –
The sprintf() function create a formatted string for one or more arguments.
Syntax –
sprintf(format, argument1, argument n);
Where,
i. format – It is required argument. It specifies the string and how the format the variable. Following
formats are –
a) % % - return % sign on web page.
b) %b – return binary No
c) %d – return decimal no
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
j) %o – return octal number.
k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
ii. argument 1,….,argument n - it represent variable or string those formatted.
Shriram Institute of Information Technology, Paniv

Example –
<?php
$n=10;
$d=sprintf("%b",$n);
$x=sprintf("%X",$n);
echo "Binray no is: ".$d;
echo "<br>Hexadecimal no is: ".$x;
?>
Output -
Decimal no is: 1010
Hexadecimal no is: A

2) printf() function –
The printf() function is used to output in a formatted string.
Syntax –
printf(format, argument 1,…,argument n);
Where,
i. format – It is required argument. It specifies the string and how the format the variable.
Following formats are –
a) % % - return % sign on web page.
b) %d – return singed decimal No
c) %b – return binary no.
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
j) %o – return octal number.
k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
ii. argument 1,….,argument n - it represent variable or string those formatted.
Shriram Institute of Information Technology, Paniv

Example -
<?php
$n=10;
$n1=20.50;
printf("%b %d",$n,$n1);
?>
Output –
1010 20

3) sscanf() function –
The sscanf() function parses of input format string according to specify format and return output
as array.
Syntax –
sscanf(string, argument 1,…,argument n);
Where,
i. string – to specify the string to read format.
ii. Format – It is required argument. It specifies the string and how the format the variable.
Following formats are –
a) % % - return % sign on web page.
b) %d – return singed decimal No
c) $b – return binary no.
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
j) %o – return octal number.
k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
Example –
<?php
$str="Good Morning";
$r=sscanf($str,"%s%s");
Shriram Institute of Information Technology, Paniv

var_dump($r);
?>
Output –
array(2)
{
[0]=> string(4) "Good"
[1]=> string(7) "Morning"
}

 Formatted string for storage –


In PHP following functions are used to format string for storage that is file or database. Function
are –
1) vprintf() function –
2) fprintf() function –
3) vfprintf() function –
4) vsprintf() function –

1) vprintf() function –
It is used to format string for output stream. vprintf() function is like printf() function, but it
accept array of argument instead of variable.
Syntax –
vprintf(format, arg_array(arg1,..,arg n));
Where,
i. Format – It is required argument. It specifies the string and how the format the variable.
Following formats are –
a) % % - return % sign on web page.
b) %d – return singed decimal No
c) %b – return binary no.
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
Shriram Institute of Information Technology, Paniv

j) %o – return octal number.


k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
ii. arg_array – Array variable.
Example –
<?php
$a=89;
vprintf("%c",array($a));
?>
Output –
Y

2) fprintf() function –
The fprintf() function is used to write a formatted string to a stream (file).
Syntax –
fprintf(stream, format, arg1,…,agr n);
Where,
i. stream – to specify the name of stream or file.
ii. Format – It is required argument. It specifies the string and how the format the variable.
Following formats are –
a) % % - return % sign on web page.
b) %d – return singed decimal No
c) %b – return binary no.
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
j) %o – return octal number.
k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
iii. Arg1,…arg n –
Shriram Institute of Information Technology, Paniv

Example -
<?php
$a=15;
$f=fopen("demo.txt","w");
fprintf($f,"%f",$a);
echo "Data add in file";
?>
Output –
Data add in file

3) vfprintf() function –
The vfprintf() function write formatted stream to a specified output stream that is file or
database. Its argument placed in an array.
Syntax –
vfprintf(stream, format, arg_array(value1, value n));
Where,
i. stream – to specify the name of stream or file.
ii. Format – It is required argument. It specifies the string and how the format the variable.
Following formats are –
a) % % - return % sign on web page.
b) %d – return singed decimal No
c) %b – return binary no.
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
j) %o – return octal number.
k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
iii. Arg_array(val1, … val n)) – array variable.
Shriram Institute of Information Technology, Paniv

Example –
<?php
$a=15;
$b=98;
$c=15;
$f=fopen("demo.txt","w");
vfprintf($f,"%b%c%x",array($a,$b,$c));
echo "Data Insert in file";
?>
Output -
Data Insert in file

4) vsprintf() function –
This function is used to write formatted string to a variable. The arguments are placed in array.
Syntax –
vsprintf(format, array_arg);
Where,
i. Format – It is required argument. It specifies the string and how the format the variable.
Following formats are –
a) % % - return % sign on web page.
b) %d – return singed decimal No
c) %b – return binary no
d) %c – return a character according to ASCII value.
e) % e – return a scientific notation using lower case.
f) %E – return a scientific notation using upper case.
g) %f – return floating point number.
h) %F – return floating point number.
i) %q – return shorter of %e and %f
j) %o – return octal number.
k) %s – return string.
l) %x – return Hexadecimal number with lower letter.
m) %X – return Hexadecimal number with upper case letter.
ii. Arg_array(val1, … val n)) – array variable.
Shriram Institute of Information Technology, Paniv

Example –
<?php
$a=15;
$b=10;
$s=vsprintf("%x%x",array($a,$b));
echo $s;
?>
Output –
fa

 Joining and splitting strings –


PHP support joining string with array and splitting string into array.
To perform joining string use -
1) join() function –
To perform splitting string use -
2) str_split() function –
3) chunk_split() function –

1) join() function –
It is used to join array elements with string.
Syntax –
join(separator, array);
Example –
<?php
$a=array("Welcome","in","PHP","World");
echo join(" ",$a);
echo "<br>";
echo join(" = ",$a);
?>
Output –
Welcome in PHP World
Welcome = in = PHP = World
Shriram Institute of Information Technology, Paniv

2) str_split() function –
This function is used to divide the string into array. Its division is based on specific number of
character.

Syntax –
str_split(string, length);
Example –
<?php
$str="PHP Programming";
print_r(str_split($str,5));
?>
Output –
Array ( [0] => PHP P [1] => rogra [2] => mming )

3) chunk_split() –
This function is used to split string into series of smaller part.
Syntax –
chunk_split(string, length, end);
Example –
<?php
$str="PHP Programming";
print_r(chunk_split($str,5," = "));
?>
Output –
PHP P = rogra = mming =

 Comparing String –
PHP support following functions to comparing string –
1) strcmp() function –
2) strcasecmp() function –
3) strnatcmp() function –
4) strnatcasecmp() function –
5) substr_compare() function –
Shriram Institute of Information Technology, Paniv

1) strcmp() function –
The strcmp() function is used to compare two strings. The comparison is based on ASCII values.
These function is case sensitive function. It returns following values –
a) It returns 0 (zero) if both strings are equal.
b) It returns a negative number (<0), if first string is smaller than second string.
c) It returns a positive value (>0), if first string is greater than second string.
Syntax –
strcmp(“string1”, “string2”);
Example -
<?php
$a="Hello";
$b="hello";
echo "Result is: ".strcmp($a,$b);
?>
Output –
Result is: -1
2) strcasecmp() function –
The strcasecmp() function is used to compare two strings. The comparison is based on ASCII
values. These function is not case sensitive function. It returns following values –
a) It returns 0 (zero) if both strings are equal.
b) It returns a negative number (<0), if first string is smaller than second string.
c) It returns a positive value (>0), if first string is greater than second string.
Syntax –
strcasecmp(“string1”, “string2”);

Example -
<?php
$a="Hello";
$b="hello";
echo "Result is: ".strcasecmp($a,$b);
?>
Output –
Result is: 0
Shriram Institute of Information Technology, Paniv

3) strnatcmp() function –
The strnatcmp() function is used to compare two string using natural algorithm. In natural
algorithm number 2 is less than number 10. It is case sensitive function. It return following –
a) It returns 0 (zero) if both strings are equal.
b) It returns a negative number (<0), if first string is smaller than second string.
c) It returns a positive value (>0), if first string is greater than second string.
Syntax –
strnatcmp(string1, string 2);
Example –
<?php
$a="1 Hello";
$b="10 Hello";
echo "Result is: ".strnatcmp($a,$b);
?>
Output –
Result is: -1

4) strnatcasecmp() function –
The strnatcasecmp() function is used to compare two string using natural algorithm. In natural
algorithm number 2 is less than number 10. It is case sensitive function. It return following –
a) It returns 0 (zero) if both strings are equal.
b) It returns a negative number (<0), if first string is smaller than second string.
c) It returns a positive value (>0), if first string is greater than second string.
Syntax –
strnatcasecmp(string1, string 2);
Example –
<?php
$a="1 Hello";
$b="10 Hello";
echo "Result is: ".strnatcasecmp($a,$b);
?>
Output –
Result is: -1
Shriram Institute of Information Technology, Paniv

5) substr_compare() function –
The substr_compare() function is used to compare two string from a specified start position. It
return following
a) It returns 0 (zero) if both strings are equal.
b) It returns a negative number (<0), if first string is smaller than second string.
c) It returns a positive value (>0), if first string is greater than second string.
Syntax –
substr_compare(string1, string 2, start, length, case);
Example –
<?php
$a="Hello";
$b="Hello";
echo "Result is: ".substr_compare($a,$b,2);
?>
Output -
Result is: 1

 Matching and Replacing substring / pattern -


A) Matching substring / pattern –
PHP provide the following function to match substring or pattern .
1) substr() function –
2) strpos() function –
3) stripos() function –

1) substr() function –
The substr() function is used to return a part of string.
Syntax –
substr(string, start, length);
Where,
a) String – to specify the string to return a part.
b) Start – to specify where to start in string.
a. +ve no – start at specified position from starting of string.
b. –ve no – start at specified position from end of string.
c. 0 (zero) – start at first character.
Shriram Institute of Information Technology, Paniv

c) Length – to specify the length of the returned string.


a. +ve no - Length to return from start of string.
b. –ve no – length to return from end of string.
c. 0 (zero) – it return empty string.
Example –
<?php
$a="Welcome in PHP";
echo substr($a,6,4);
?>
Output –
e in
2) strpos() function –
The strops() function is used to finds the position of first occurrence of string inside another
string. It is case sensitive function.
Syntax –
strops(string, find, start);
Example –
<?php
$a="Welcome in PHP";
echo strpos($a,"in");
?>
Output –
8
3) stripos() function –
The stripos() function is used to finds the position of first occurrence of string inside another
string. It is not case sensitive function.
Syntax –
stripos(string, find, start);
Example –
<?php
$a="Welcome in PHP";
echo stripos($a,"In");
?>
Output –
8
Shriram Institute of Information Technology, Paniv

B) Replacing substring / pattern –


Following function provide in PHP to replacing substring or pattern.
1) str_replace() function –
2) str_ireplace() function –
3) substr_replace() function –
1) str_replace() function –
The str_replace() function is used to replace some character with another character. These
function is case sensitive.
Syntax –
str_replace(find, replace, string, count);
Example –
<?php
$a="Welcome in PHP";
echo str_replace("PHP","SIIT",$a);
?>
Output –
Welcome in SIIT

2) str_ireplace() function –
The str_ireplace() function is used to replace some character with another character. These
function is not case sensitive.
Syntax –
str_ireplace(find, replace, string, count);
Example –
<?php
$a="Welcome in PHP";
echo str_ireplace("php","SIIT",$a);
?>
Output –
Welcome in SIIT
3) substr_replace() function –
The substr_replace() function is used to find and replace a part of string using another string.
Syntax –
substr_replace(string, replace, start, length);
Shriram Institute of Information Technology, Paniv

Example –
<?php
$a="Welcome in PHP";
echo substr_replace($a,"SIIT",8);
?>
Output –
Welcome SIIT
Basic Regular Expression –
Regular expression is basically pattern matching inside text; they use special syntax and concept
in order to obtain information from a string. Regular expression is nothing more than sequence or pattern of
character itself.
In PHP there are two types of regular expression.
1) POSIX regular expression
2) PERL regular expression
The following table shows some regular expression and the corresponding string which matches the
regular expression pattern.
Sr.No Expression Description
1 BCA Display BCA string.
2 ^BCA The string start with BCA
3 BCA$ The string end with BCA
4 [abc] a, b or c
5 [a-z] Lowercase letter any between a to z
6 [a-z]+ One or more lowercase letters
7 [^A-Z] Any letter which is not uppercase letter.
8 \s Matches space characters
9 \d Matches any digit from 0 to 9
Matches word characters including all lower and
10 \w
uppercase case letters, digits, and underscore.
11 B+ Matches any string that contain at least one B
Matches any string that contains zero or more
12 B*
occurrences of B
Matches any string that contains zero or more
13 B?
occurrences of B
14 B{n} Matches any string that contains a sequence of X n's
Shriram Institute of Information Technology, Paniv

The following library function is used with regular expression.


1) preg_match() function –
The preg_match() function is used to for search string for pattern, it return true if pattern is exists
otherwise it return false.
Syntax –
preg_match(pattern, string);
Example –
<?php
$str="Welcome in PHP";
$m=preg_match("/P*/",$str);
echo $m;
?>
Output –
1
2) preg_match_all() function –
These function is used to matches all occurrences of pattern in string and display in array format.
Syntax –
preg_match_all(pattern, string, $array);
Example –
<?php
$str="Welcome in PHP";
$m=preg_match_all("/e/",$str);
echo $m;
?>
Output –
2

3) preg_replace() function –
The preg_replace() function is used to perform a regular expression for search and replace the
content.
Syntax –
preg_replace(pattern, replacement, string, limit, count);
Example –
<?php
$str="Welcome in PHP";
Shriram Institute of Information Technology, Paniv

$m=preg_replace("/PHP/","SIIT",$str);
echo $m;
?>
Output –
Welcome in SIIT
4) preg_split() function –
The preg_split() function is used to convert the given string into an array. The function splits the
string into smaller strings.
Syntax –
preg_split(pattern, string, limit, flag);
Example –
<?php
$str="Welcome in PHP";
$m=preg_split("/[\s,]+/",$str);
print_r ($m);
?>
Output –
Array(
[0] => Welcome
[1] => in
[2] => PHP
)
5) preg_grep() function –
The preg_grep() function returns the array consisting of elements of the input array that match
with given pattern.
Syntax –
preg_grep(pattern, array, flag);
Example –
<?php
$str =array("Red","Pink","Green","Blue","Purple");
$r = preg_grep("/^p/i", $str);
print_r($r);
?>
Shriram Institute of Information Technology, Paniv

Output –
Array (
[1] => Pink
[4] => Purple
)
6) ereg() function –
The erge() function is used to search a string specified by string for pattern. It is case sensitive
function.
Syntax –
ereg(pattern, original string, array);
Example –
<?php
$email_id = "admin@BCA.com";
$retval =ereg("(\.)(com*)", $email_id);
if( $retval == true )
{
echo "Found a .com<br>";
}
else
{
echo "Could not found a .com<br>";
}
?>
Output –
Found a .com

7) eregi() function –
The eregi() function is used to search a string specified by string for pattern. It is case sensitive
function.
Syntax –
eregi(pattern, original string, array);
Example –
<?php
$email_id = "admin@BCA.com";
$retval =eregi("(\.)(com*)", $email_id);
Shriram Institute of Information Technology, Paniv

if( $retval == true )


{
echo "Found a .com<br>";
}
else
{
echo "Could not found a .com<br>";
}
?>
Output –
Found a .com

8) ereg_replace() function –
The ereg_replace() function is used to search for string specified by pattern and replace pattern
with replacement it found.
Syntax-
ereg_replace(pattern, replacement, string);
Example –
<?php
$str="Copyright 1990";
$r=ereg_replace("[0-9]+","2000",$str);
echo $r;
?>
Output –
Copyright 2000

You might also like