E-Notes: T.Y.B.Sc. (Computer Science)
E-Notes: T.Y.B.Sc. (Computer Science)
for
T.Y.B.Sc.(Computer Science)
Subject of
Internet Programming I (CS-334)
Prepared By
Assistant Professor I. A. Ansari
M.S.G. Arts, Science & Commerce College Malegaon camp,
Malegaon(Nsk.)
Unit 2
Functions and Strings
The statement list can include HTML. You can declare a PHP function that doesn’t contain any PHP
code. The function name can be any string that starts with a letter or underscore followed by zero or
more letters, underscores, and digits. Function names are case-insensitive; that is, you can call the
sin( ) function as sin(1), SIN(1), SiN(1), and so on, because all these names refer to the same function.
Typically, functions return some value. To return a value from a function, use the return statement. If
you define your function with the optional ampersand before the name, the function returns a
reference to the returned data rather than a copy of the data.
Following example takes two strings, concatenates them, and then returns the result. The function
takes two arguments, $left and $right. Using the concatenation operator, the function creates a
combined string in the variable $combined_string.
<?php
function strcat($left, $right)
{
return $left . $right;
}
When you call get_preferences( ), you can choose to supply an argument. If you do, it returns the
preference matching the string you give it; if not, it returns all preferences. A function may have any
number of parameters with default values. However, they must be listed after all the parameters that
do not have default values.
function get_preferences( ) {
// some code
}
PHP provides three functions you can use in the function to retrieve the parameters passed to it.
func_get_args( ) returns an array of all parameters provided to the function, func_num_args( ) returns the
number of parameters provided to the function, and func_get_arg( ) returns a specific argument from
the parameters.
$array = func_get_args( );
$count = func_num_args( );
$value = func_get_arg(argument_number);
when you call a function, you can pass any number of arguments to the function. Any parameters the
function expects that are not passed to it remain unset, and a warning is issued for each of them
<?php
function takes_two( $a, $b ) {
if (isset($a)) { echo " a is set\n"; }
if (isset($b)) { echo " b is set\n"; }
}
?>
With two arguments:
a is set
b is set
In this case, we could use a variable function call to call the appropriate function. To make a variable
function call, include the parameters for a function in parentheses after the variable. To rewrite the
previous example:
$which(); // if $which is "first" the function first( ) is called, etc...
You can create an anonymous function or lambda function using create_function( ). This function takes
two parameters—the first describes the parameters the anonymous function takes in, and the second
is the actual code. A randomly generated name for the function is returned:
The array is sorted by usort( ), using the anonymous function, in order of string length.
Single-Quoted Strings
Single-quoted strings do not interpolate variables. Thus, the variable name in the following string is
not expanded because the string literal in which it occurs is single quoted.
$name = 'Fred';
$str = 'Hello, $name'; // single-quoted
echo $str;
Hello, $name
Double-Quoted Strings
Double-quoted strings interpolate variables and expand the many PHP escape sequences. Table lists
the escape sequences recognized by PHP in double-quoted strings.
Escape sequence Character represented
\" Double quotes
\n New Line
\r Carriage return
\t Tab
\\ Backslash
\$ Dollar Sign
\{ Left brace
\} Right brace
\[ Left bracket
\] Right bracket
\0 through \777 ASCII character represented by octal value
\x0 through \xFF ASCII character represented by hex value
Here Documents
You can easily put multiline strings into your program with a heredoc, as follows,
echo $clerihew;
The <<< Identifier tells the PHP parser that you’re writing a heredoc. There must be a space after the
<<< and before the identifier. You get to pick the identifier. The next line starts the text being quoted
by the heredoc, which continues until it reaches a line that consists of nothing but the identifier.
Variable Interpolation
When you define a string literal using double quotes or a heredoc, the string is subject to variable
interpolation. Interpolation is the process of replacing variable names in the string with the values of
those variables. There are two ways to interpolate variables into strings—the simple way and the
complex way. The simple way is to just put the variable name in a double-quoted string or heredoc:
$who = 'Kilroy';
$where = 'here';
$n = 12;
echo "You are the {$n}th person";
You are the 12th person
Without the curly braces, PHP would try to print the value of the $nth variable.
echo
To put a string into the HTML of a PHP-generated page, use echo.
You can specify multiple items to print by separating them with commas,
print( )
The print( ) function sends one value to the browser. It returns true if the string was successfully
displayed and false otherwise.
if (! print("Hello, world")) {
die("you're not listening to me!");
}
Hello, world
printf( )
The printf( ) function outputs a string built by substituting values into the format string. It is derived from
the function of the same name in the standard C library. The first argument to printf( ) is the format
string. The remaining arguments are the values to be substituted in. A % character in the format string
indicates a substitution. For example,
• Formatting a date:
var_dump( ) is preferable to print_r( ) for debugging. The var_dump( ) function displays any PHP value in
a human-readable format,
HTML
Special characters in HTML are represented by entities such as & and <. There are two PHP
functions for turning special characters in a string into their entities, one for removing HTML tags, and
one for extracting only meta tags.
The entity-escaped version (ü) correctly displays as ü in the web page. As you can see, the
space has not been turned into . The htmlentities( ) function actually takes up to three
arguments:
$output = htmlentities(input, quote_style, charset);
If you have an application that displays data that a user has entered in a form, you need to run that
data through htmlspecialchars( ) before displaying or saving it. If you don’t, and the user enters a string
like "angle < 30" or "sturm & drang", the browser will think the special characters are HTML, and you’ll
have a garbled page.
The function may take a second argument that specifies a string of tags to leave in the string. List only
the opening forms of the tags. The closing forms of tags listed in the second parameter are also
preserved:
$input = 'The <b>bold</b> tags will <i>stay</i><p>';
$output = strip_tags($input, '<b>');
// $output is 'The <b>bold</b> tags will stay'
Extracting meta tags
If you have the HTML for a web page in a string, the get_meta_tags( ) function returns an array of the
meta tags in that page. The name of the meta tag (keywords, author, description, etc.) becomes the key
in the array, and the content of the meta tag becomes the corresponding value:
$meta_tags = get_meta_tags('http://www.example.com/');
echo "Web page made by {$meta_tags[author]}";
Web page made by John Doe
Pass a true value for use_include_path to let PHP attempt to open the file using the standard include
path.
URL
To encode a string according to the URL conventions, use rawurlencode( ):
$output = rawurlencode(input);
This function takes a string and returns a copy with illegal URL characters encoded in the %dd
convention. If you are dynamically generating hypertext references for links in a page, you need
to convert them with rawurlencode( ),
$encoded = 'Programming%20PHP';
echo rawurldecode($encoded);
Programming PHP
SQL
Most database systems require that string literals in your SQL queries be escaped. SQL’s encoding
scheme is pretty simple—single quotes, double quotes, NUL-bytes, and backslashes need to be
preceded by a backslash. The addslashes( ) function adds these slashes, and the stripslashes( ) function
removes them:
echo addslashes($string);
echo stripslashes($string);
$o1 = 3;
$o2 = "3";
if ($o1 == $o2) {
echo("== returns true<br>");
}
if ($o1 === $o2) {
echo("=== returns true<br>");
}
== returns true
To explicitly compare two strings as strings, casting numbers to strings if necessary, use the strcmp( )
function:
The function returns a number less than 0 if string_1 sorts before string_2, greater than 0 if string_2
sorts before string_1, or 0 if they are the same:
$n = strcmp("PHP Rocks", 5);
echo($n);
1
A variation on strcmp( ) is strcasecmp( ), which converts strings to lowercase before comparing them. Its
arguments and return values are the same as those for strcmp( ),
$n = strcasecmp("Fred", "frED"); // $n is 0
The final variation on these functions is natural-order comparison with strnatcmp( ) and strnatcasecmp( ),
which take the same arguments as strcmp( ) and return the same kinds of values. Natural-order
comparison identifies numeric portions of the strings being compared and sorts the string parts
separately from the numeric parts.
Approximate Equality
PHP provides several functions that let you test whether two strings are approximately equal: soundex(
), metaphone( ), similar_text(), and levenshtein( ).
$soundex_code = soundex($string);
$metaphone_code = metaphone($string);
$in_common = similar_text($string_1, $string_2 [, $percentage ]);
$similarity = levenshtein($string_1, $string_2);
$similarity = levenshtein($string_1, $string_2 [, $cost_ins, $cost_rep, $cost_del ]);
The Soundex and Metaphone algorithms each yield a string that represents roughly how a word is
pronounced in English. To see whether two strings are approximately equal with these algorithms,
compare their pronunciations. You can compare Soundex values only to Soundex values and
Metaphone values only to Metaphone values. The Metaphone algorithm is generally more accurate.
The similar_text( ) function returns the number of characters that its two string arguments have in
common.
The Levenshtein algorithm calculates the similarity of two strings based on how many characters you
must add, substitute, or remove to make them the same. For instance, "cat" and "cot" have a
Levenshtein distance of 1, because you need to change only one character (the "a" to an "o") to make
them the same:
The function replaces the part of original indicated by the start (0 means the start of the string) and
length values with the string new. If no fourth argument is given, substr_replace( ) removes the text from
start to the end of the string.For instance:
A negative value for start indicates the number of characters from the end of the string from which to
start the replacement:
A negative length indicates the number of characters from the end of the string at which to stop
deleting:
The strrev( ) function takes a string and returns a reversed copy of it:
$string = strrev(string);
For example:
echo strrev("There is no cabal");
labac on si erehT
The str_repeat( ) function takes a string and a count and returns a new string consisting of the
argument string repeated count times:
The str_pad( ) function pads one string with another. Optionally, you can say what string to pad with,
and whether to pad on the left, right, or both.
Decomposing a String
PHP provides several functions to let you break a string into smaller components. In
increasing order of complexity.
explode( ) function:
The first argument, separator, is a string containing the field separator. The second argument, string, is
the string to split. The optional third argument, limit, is the maximum number of values to return in the
array. If the limit is reached, the last element of the array contains the remainder of the string:
$input = 'Fred,25,Wilma';
$fields = explode(',', $input);
// $fields is array('Fred', '25', 'Wilma')
The implode( ) function does the exact opposite of explode( )—it creates a large string from an array of
smaller strings:
$string = implode(separator, array);
The first argument, separator, is the string to put between the elements of the second argument, array.
To reconstruct the simple comma-separated value string, simply say:
The strtok( ) function lets you iterate through a string, getting a new chunk (token) each time. The first
time you call it, you need to pass two arguments: the string to iterate over and the token separator:
$first_chunk = strtok(string, separator);
To retrieve the rest of the tokens, repeatedly call strtok( ) with only the separator:
$next_chunk = strtok(separator);
$string = "Fred,Flintstone,35,Wilma";
$token = strtok($string, ",");
while ($token !== false) {
echo("$token<br>");
$token = strtok(",");
}
Fred
Flintstone
35
Wilma
The strtok( ) function returns false when there are no more tokens to be returned.
print_r($a);
Array
(
[0] => Fred
[1] => Flintstone
[2] => 35
)
String-Searching Functions
Several functions find a string or character within a larger string. They come in three families: strpos( )
and strrpos( ), which return a position; strstr( ), strchr( ), and friends, which return the string they find;
and strspn( ) and strcspn( ), which return how much of the start of the string matches a mask.
The strpos( ) function finds the first occurrence of a small string in a larger string:
$position = strpos(large_string, small_string);
If the small string isn’t found, strpos( ) returns false. The strrpos( ) function finds the last occurrence of a
character in a string. It takes the same arguments and returns the same type of value as strpos( ).
For instance:
$record = "Fred,Flintstone,35,Wilma";
$pos = strrpos($record, ","); // find last comma
$record = "Fred,Flintstone,35,Wilma";
$rest = strstr($record, ","); // $rest is ",Flintstone,35,Wilma"
For example, this function tests whether a string holds an octal number:
The c in strcspn( ) stands for complement—it tells you how much of the start of the string is not
composed of the characters in the character set.
Decomposing URLs
$array = parse_url(url);
For example:
$bits = parse_url('http://me:[email protected]/cgi-bin/board?user=fred);
print_r($bits);
Array
(
[scheme] => http
[host] => example.com
[user] => me
[pass] => secret
[path] => /cgi-bin/board
[query] => user=fred
)
2.10 Regular expressions
A regular expression is a string that represents a pattern. The regular expression functions compare
that pattern to another string and see if any of the string matches the pattern. Some functions tell you
whether there was a match, while others make changes to the string.
PHP provides support for two different types of regular expressions: POSIX and Perl compatible.
POSIX regular expressions are less powerful, and sometimes slower, than the Perl-compatible
functions, but can be easier to read. There are three uses for regular expressions: matching,
substituting and splitting a string. PHP has functions for all three behaviors for both Perl and POSIX
regular expressions.
The Basics
Most characters in a regular expression are literal characters, meaning that they match only
themselves. For instance, if you search for the regular expression "cow" in the string "Dave was a
cowhand", you get a match because "cow" occurs in that string. Some characters, though, have special
meanings in regular expressions. For instance, a caret (^) at the beginning of a regular expression
indicates that it must match the beginning of the string.
Similarly, a dollar sign ($) at the end of a regular expression means that it must match the end of the
string.
Regular expressions are case-sensitive by default, so the regular expression "cow" doesn’t match the
string "COW". If you want to perform a case-insensitive POSIX-style match, you can use the eregi( )
function. With Perl-style regular expressions, you still use preg_match( ), but specify a flag to indicate a
case-insensitive match.
Character Classes
To specify a set of acceptable characters in your pattern, you can either build a character class
yourself or use a predefined one. You can build your own character class by enclosing the acceptable
characters in square brackets:
The regular expression engine finds a "c", then checks that the next character is one of "a", "e", "i", "o",
or "u". If it isn’t a vowel, the match fails and the engine goes back to looking for another "c". If a vowel
is found, though, the engine then checks that the next character is a "t". If it is, the engine is at the end
of the match and so returns true.
In this case, the regular expression engine is looking for a "c", followed by a character that isn’t a
vowel, followed by a "t".
You can define a range of characters with a hyphen (-). This simplifies character classes like “all
letters” and “all digits”:
The various regular expression libraries define shortcuts for character classes, including digits,
alphabetic characters, and whitespace. The actual syntax for these shortcuts differs between POSIX-
style and Perl-style regular expressions. For instance, with POSIX, the whitespace character class is
"[[:space:]]", while with Perl it is "\s".
Alternatives
You can use the vertical pipe (|) character to specify alternatives in a regular expression:
ereg('cat|dog', 'the cat rubbed my legs'); // returns true
ereg('cat|dog', 'the dog rubbed my legs'); // returns true
ereg('cat|dog', 'the rabbit rubbed my legs'); // returns false
The precedence of alternation can be a surprise: '^cat|dog$' selects from '^cat' and 'dog$', meaning that
it matches a line that either starts with "cat" or ends with "dog". If you want a line that contains just "cat"
or "dog", you need to use the regular expression '^(cat|dog)$'.
Repeating Sequences
To specify a repeating pattern, you use something called a quantifier. The quantifier goes after the
pattern that’s repeated and says how many times to repeat that pattern. Table shows the quantifiers
that are supported by both POSIX and Perl regular expressions.
Quantifier Meaning
? 0 or 1
* 0 or more
+ 1 or more
{n} Exactly n times
{n,m} At least n, no more than m times
{n,} At least n times
To repeat a single character, simply put the quantifier after the character:
Subpatterns
You can use parentheses to group bits of a regular expression together to be treated as a single unit
called a subpattern:
ereg('a (very )+big dog', 'it was a very very big dog'); // returns true
ereg('^(cat|dog)$', 'cat'); // returns true
ereg('^(cat|dog)$', 'dog'); // returns true
Matching Matching
The ereg( ) function takes a pattern, a string, and The preg_match( ) function performs Perl-style
an optional array. It populates the array, if given, pattern matching on a string. It’s the equivalent
and returns true or false depending on whether a
of the m// operator in Perl. The preg_match( )
match for the pattern
was found in the string: function takes the same arguments and gives the
same return value as the ereg( ) function, except
$found = ereg(pattern, string [, captured ]); that it takes a Perl-style pattern instead of a
For example: standard pattern:
$found = preg_match(pattern, string [, captured
ereg('y.*e$', 'Sylvie'); // returns true ]);
ereg('y(.*)e$', 'Sylvie', $a);
// returns true, $a is array('Sylvie', 'lvi')
For example:
preg_match('/y.*e$/', 'Sylvie'); // returns true
preg_match('/y(.*)e$/', Sylvie', $m);
// $m is array('Sylvie', 'lvi')
The eregi( ) function is a case-insensitive form of While there’s an eregi( ) function to match case-
ereg( ). insensitively, there’s no preg_ matchi( ) function.
Instead, use the i flag on the pattern:
preg_match('y.*e$/i', 'SyLvIe'); // returns true
Replacing Replacing
The ereg_replace( ) function takes a pattern, a The preg_replace( ) function behaves like the
replacement string, and a string in which to search and replace operation in your text editor.
search. It returns a copy of the search string, with
It finds all occurrences of a pattern in a string
text that matched the pattern replaced with the
replacement string: and changes those occurrences to something
else:
$changed = ereg_replace(pattern, replacement, string); $new = preg_replace(pattern, replacement,
If the pattern has any grouped subpatterns, the subject [, limit ]);
matches are accessible by putting the characters
\1 through \9 in the replacement string. For The most common usage has all the argument
example, we can use ereg_replace( ) to replace strings, except for the integer limit. The limit is
characters wrapped with [b] and [/b] tags with the maximum number of occurrences of the
equivalent HTML tags: pattern to replace (the default, and the behavior
$string = 'It is [b]not[/b] a matter of diplomacy.'; when a limit of -1 is passed, is all occurrences).
echo ereg_replace ('\[b]([^]]*)\[/b]', '<b>\1</b>', $string);
$better = preg_replace('//', '!', 'do not press the
It is <b>not</b> a matter of diplomacy. button');
// $better is 'do !not! press the button'