0% found this document useful (0 votes)
133 views16 pages

E-Notes: T.Y.B.Sc. (Computer Science)

The document provides an overview of functions and strings in PHP. It defines functions and how to define, call, return values from, and pass parameters to functions. It discusses default parameters, variable parameters, and anonymous functions. It also covers the different types of strings in PHP - single quoted, double quoted, and heredoc strings - and how they handle escaping sequences and variable interpolation.

Uploaded by

Rohit Kapadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views16 pages

E-Notes: T.Y.B.Sc. (Computer Science)

The document provides an overview of functions and strings in PHP. It defines functions and how to define, call, return values from, and pass parameters to functions. It discusses default parameters, variable parameters, and anonymous functions. It also covers the different types of strings in PHP - single quoted, double quoted, and heredoc strings - and how they handle escaping sequences and variable interpolation.

Uploaded by

Rohit Kapadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

E-notes

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

2.1Defining and calling a function

2.2 Default parameters

2.3 Variable parameters, Missing parameters

2.4 Variable function, Anonymous function

2.5 Types of strings in PHP

2.6 Printing functions

2.7 Encoding and escaping

2.8 Comparing strings

2.9 Manipulating and searching strings

2.10 Regular expressions

2.1Defining and calling a function


A function is a named block of code that performs a specific task, possibly acting upon a set
of values(parameters) given to it and possibly returning a single value. Functions save on compile
time. Functions are compiled only once for the page. They also improve reliability by allowing you to
fix any bugs in one place, rather than everywhere you perform a task, and they improve readability by
isolating code that performs specific tasks.
To define a function, use the following syntax:

function [&] function_name ( [ parameter [, ... ] ] )


{
statement list
}

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;
}

$first = "This is a ";


$second= " complete sentence!";

echo strcat($first, $second);


?>

2.2 Default parameters


A function may need to accept a particular parameter in some cases. For example, when you call a
function to get the preferences for a site, the function may take in a parameter with the name of the
preference to retrieve. If you want to retrieve all the preferences, rather than using some special
keyword, you can just not supply an argument. This behavior works by using default arguments. To
specify a default parameter, assign the parameter value in the function declaration. The value
assigned to a parameter as a default value cannot be a complex expression; it can only be a constant.
function get_preferences($which_preference = "all" ) {
// if $which_preference is "all", return all prefs;
// otherwise, get the specific preference requested...
}

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.

2.3 Variable parameters, Missing parameters


A function may require a variable number of arguments. For example, the get_preferences( ) example
in the previous section might return the preferences for any number of names, rather than for just one.
To declare a function with a variable number of arguments, leave out the parameter block entirely.

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"; }
}

echo "With two arguments:\n";


takes_two(1, 2);
echo "With one argument:\n";
takes_two(1);

?>
With two arguments:

a is set
b is set

With one argument:

Warning: Missing argument 2 for takes_two( ) in /path/to/script.php on line 6


a is set

2.4 Variable function, Anonymous function


As with variable variables, you can call a function based on the value of a variable. For example,
consider this situation, where a variable is used to determine which of three functions to call.
switch($which) {
case 'first':
first( );
break;
case 'second':
second( );
break;
case 'third':
third( );
break;
}

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:

$func_name = create_function(args_string, code_string);

$lambda = create_function('$a,$b', 'return(strlen($a) - strlen($b));');


$array = array('really long string here, boy', 'this', 'middling length', 'larger');
usort($array, $lambda);
print_r($array);

The array is sorted by usort( ), using the anonymous function, in order of string length.

2.5 Types of strings in PHP


There are three ways to write a literal string in your program: using single quotes, double quotes, and
the here document (heredoc) format derived from the Unix shell. These methods differ in whether they
recognize special escape sequences that let you encode other characters or interpolate variables.

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,

$clerihew = <<< End_Of_Quote


Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
End_Of_Quote;

echo $clerihew;

Sir Humphrey Davy


Abominated gravy.
He lived in the odium
Of having discovered sodium.

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';

echo "$who was $where";

Kilroy was here


The complex way is to surround the variable being interpolated with curly braces. This method can be
used either to disambiguate or to interpolate array lookups. The classic use of curly braces is to
separate the variable name from surrounding text,

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

2.6 Printing functions


There are four ways to send output to the browser.

echo
To put a string into the HTML of a PHP-generated page, use echo.

following are equivalent:


echo "Printy";
echo("Printy"); // also valid

You can specify multiple items to print by separating them with commas,

echo "First", "second", "third";


Firstsecondthird

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,

• A floating-point number to two decimal places:


printf('%.2f', 27.452);
27.45

• Decimal and hexadecimal output:

printf('The hex value of %d is %x', 214, 214);


The hex value of 214 is d6

Padding an integer to three decimal places:

printf('Bond. James Bond. %03d.', 7);


Bond. James Bond. 007.

• Formatting a date:

printf('%02d/%02d/%04y', $month, $day, $year);


02/15/2002

print_r( ) and var_dump( )


The print_r( ) construct intelligently displays what is passed to it. Strings and numbers are simply
printed. Arrays appear as parenthesized lists of keys and values, prefaced by Array,

$a = array('name' => 'Fred', 'age' => 35, 'wife' => 'Wilma');


print_r($a);
Array
(
[name] => Fred
[age] => 35
[wife] => Wilma
)

var_dump( ) is preferable to print_r( ) for debugging. The var_dump( ) function displays any PHP value in
a human-readable format,

$a= array('name' => Fred, 'age' => 35);


var_dump($a);
array(2) {
["name"]=>
string(4) "Fred"
["age"]=>
int(35)
}
2.7 Encoding and escaping
PHP programs often interact with HTML pages, web addresses (URLs), and databases, there are
functions to help you work with those types of data. HTML, web page addresses, and database
commands are all strings, but they each require different characters to be escaped in different ways.
For instance, a space in a web address must be written as %20, while a literal less-than sign (<) in an
HTML document must be written as &lt;. PHP has a number of built-in functions to convert to and from
these encodings.

HTML
Special characters in HTML are represented by entities such as &amp; and &lt;. 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.

Entity-quoting all special characters


The htmlspecialchars( ) function changes all characters with HTML entity equivalents into those
equivalents (with the exception of the space character). This includes the less-than sign (<), the
greater-than sign (>), the ampersand (&), and accented characters. For example,

$string = htmlentities("Einstürzende Neubauten");


echo $string;
Einst&uuml;rzende Neubauten

The entity-escaped version (&uuml;) correctly displays as ü in the web page. As you can see, the
space has not been turned into &nbsp;. The htmlentities( ) function actually takes up to three
arguments:
$output = htmlentities(input, quote_style, charset);

The charset parameter, if given, identifies the character set.

Entity-quoting only HTML syntax characters


The htmlspecialchars( ) function converts the smallest set of entities possible to generate valid HTML.
The following entities are converted:
• Ampersands (&) are converted to &amp;
• Double quotes (") are converted to &quot;
• Single quotes (') are converted to &#039; (if ENT_QUOTES is on, as described for
htmlentities( ))
• Less-than signs (<) are converted to &lt;
• Greater-than signs (>) are converted to &gt;

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.

$output = htmlspecialchars(input, [quote_style, [charset]]);

Removing HTML tags


The strip_tags( ) function removes HTML tags from a string:

$input = '<p>Howdy, &quot;Cowboy&quot;</p>';


$output = strip_tags($input);
// $output is 'Howdy, &quot;Cowboy&quot;'

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

The general form of the function is:

$array = get_meta_tags(filename [, use_include_path]);

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( ),

$name = "Programming PHP";


$output = rawurlencode($name);
echo "http://localhost/$output";
http://localhost/Programming%20PHP

The rawurldecode( ) function decodes URL-encoded strings:

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

$string = <<< The_End


"It's never going to work," she cried,
as she hit the backslash (\\) key.
The_End;

echo addslashes($string);

\"It\'s never going to work,\" she cried,


as she hit the backslash (\\) key.

echo stripslashes($string);

"It's never going to work," she cried,


as she hit the backslash (\) key.

2.8 Comparing strings


Exact Comparisons
You can compare two strings for equality with the == and === operators. These operators differ in how
they deal with non-string operands. The == operator casts nonstring operands to strings, so it reports
that 3 and "3" are equal. The === operator does not cast, and returns false if the types of the
arguments differ.

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

$relationship = strcmp(string_1, string_2);

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:

$similarity = levenshtein("cat", "cot"); // $similarity is 1

2.9 Manipulating and searching strings


Substrings
If you know where in a larger string the interesting data lies, you can copy it out with the substr( )
function:
$piece = substr(string, start [, length ]);
The start argument is the position in string at which to begin copying, with 0 meaning the start of the
string. The length argument is the number of characters to copy (the default is to copy until the end of
the string). For example:

$name = "Fred Flintstone";


$fluff = substr($name, 6, 4); // $fluff is "lint"
$sound = substr($name, 11); // $sound is "tone"
To learn how many times a smaller string occurs in a larger one, use substr_count( ):
$number = substr_count(big_string, small_string);

The substr_replace( ) function permits many kinds of string modifications:


$string = substr_replace(original, new, start [, length ]);

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:

$greeting = "good morning citizen";


$farewell = substr_replace($greeting, "bye", 5, 7);
// $farewell is "good bye citizen"

Use a length value of 0 to insert without deleting:

$farewell = substr_replace($farewell, "kind ", 9, 0);


// $farewell is "good bye kind citizen"

Use a replacement of "" to delete without inserting:

$farewell = substr_replace($farewell, "", 8);


// $farewell is "good bye"

Here’s how you can insert at the beginning of the string:

$farewell = substr_replace($farewell, "now it's time to say ", 0, 0);


// $farewell is "now it's time to say good bye"'

A negative value for start indicates the number of characters from the end of the string from which to
start the replacement:

$farewell = substr_replace($farewell, "riddance", -3);


// $farewell is "now it's time to say good riddance"

A negative length indicates the number of characters from the end of the string at which to stop
deleting:

$farewell = substr_replace($farewell, "", -8, -5);


// $farewell is "now it's time to say good dance"

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:

$repeated = str_repeat(string, count);

For example, to build a crude horizontal rule:


echo str_repeat('-', 40);

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.

$padded = str_pad(to_pad, length [, with [, pad_type ]]);

The default is to pad on the right with spaces:

$string = str_pad('Fred Flintstone', 30);


echo "$string:35:Wilma";
Fred Flintstone :35:Wilma

The optional third argument is the string to pad with:


$string = str_pad('Fred Flintstone', 30, '. ');
echo "{$string}35";
Fred Flintstone. . . . . . . .35

Decomposing a String
PHP provides several functions to let you break a string into smaller components. In
increasing order of complexity.

explode( ) function:

$array = explode(separator, string [, limit]);

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:

$fields = array('Fred', '25', 'Wilma');


$string = implode(',', $fields); // $string is 'Fred,25,Wilma'

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);

For instance, consider this invocation:

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

The sscanf( ) function decomposes a string according to a printf( )-like template:


$array = sscanf(string, template);
$count = sscanf(string, template, var1, ... );

If used without the optional variables, sscanf( ) returns an array of fields:

$string = "Fred\tFlintstone (35)";


$a = sscanf($string, "%s\t%s (%d)");

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

Searches returning rest of string


The strstr( ) function finds the first occurrence of a small string in a larger string and returns from that
small string on. For instance:

$record = "Fred,Flintstone,35,Wilma";
$rest = strstr($record, ","); // $rest is ",Flintstone,35,Wilma"

The variations on strstr( ) are:


stristr( ) : Case-insensitive strstr( )
strchr( ) : Alias for strstr( )
strrchr( ) : Find last occurrence of a character in a string

Searches using masks


If you thought strrchr( ) was esoteric, you haven’t seen anything yet. The strspn( ) and strcspn( )
functions tell you how many characters at the beginning of a string are comprised of certain
characters:

$length = strspn(string, charset);

For example, this function tests whether a string holds an octal number:

function is_octal ($str) {


return strspn($str, '01234567') == strlen($str);
}

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

The parse_url( ) function returns an array of components of a URL:

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

ereg('^cow', 'Dave was a cowhand'); // returns false


ereg('^cow', 'cowabunga!'); // returns true

Similarly, a dollar sign ($) at the end of a regular expression means that it must match the end of the
string.

ereg('cow$', 'Dave was a cowhand'); // returns false


ereg('cow$', "Don't have a cow"); // returns true

A period (.) in a regular expression matches any single character:


ereg('c.t', 'cat'); // returns true
ereg('c.t', 'cut'); // returns true
ereg('c.t', 'c t'); // returns true
ereg('c.t', 'bat'); // returns false
ereg('c.t', 'ct'); // returns false

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:

ereg('c[aeiou]t', 'I cut my hand'); // returns true


ereg('c[aeiou]t', 'This crusty cat'); // returns true
ereg('c[aeiou]t', 'What cart?'); // returns false
ereg('c[aeiou]t', '14ct gold'); // returns false

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.

You can negate a character class with a caret ( ^) at the start:


ereg('c[^aeiou]t', 'I cut my hand'); // returns false
ereg('c[^aeiou]t', 'Reboot chthon'); // returns true
ereg('c[^aeiou]t', '14ct gold'); // returns false

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

ereg('[0-9]%', 'we are 25% complete'); // returns true


ereg('[0123456789]%', 'we are 25% complete'); // returns true
ereg('[a-z]t', '11th'); // returns false
ereg('[a-z]t', 'cat'); // returns true
ereg('[a-z]t', 'PIT'); // returns false
ereg('[a-zA-Z]!', '11!'); // returns false
ereg('[a-zA-Z]!', 'stop!'); // returns true

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:

ereg('ca+t', 'caaaaaaat'); // returns true


ereg('ca+t', 'ct'); // returns false
ereg('ca?t', 'caaaaaaat'); // returns false
ereg('ca*t', 'ct'); // returns true

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

POSIX Style R.E. PCRE (Perl Compatible R.E.)


Character Classes Character Classes
As shown in Table, POSIX defines a number of Perl-style regular expressions support the POSIX
named sets of characters that you can use in character classes but also define some of their
character classes.
own, as shown in Table.
Anchors Anchors
An anchor limits a match to a particular location Perl-style regular expressions also support
in the string . Table the anchors supported by additional anchors, as listed in Table.
POSIX regular expressions.

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'

Pass an array of strings as subject to make the


substitution on all of them. The new strings are
The eregi_replace( ) function is a case-insensitive returned from preg_replace( ):
form of ereg_replace( ). Its arguments
and return values are the same as those for $names = array('Fred Flintstone', 'Barney
ereg_replace( ).
Rubble', 'Wilma Flintstone', 'Betty Rubble');

$tidy = preg_replace('/(\w)\w* (\w+)/', '\1 \2',


$names);

// $tidy is array ('F Flintstone', 'B Rubble', 'W


Flintstone', 'B Rubble') .
Splitting Splitting
The split( ) function uses a regular expression to use preg_split( ) to extract chunks when you
divide a string into smaller chunks, which are know what separates the chunks from each
returned as an array. If an error occurs, split( ) other:
returns false. Optionally, you can say how many
chunks to return:
$chunks = preg_split(pattern, string [, limit [,
$chunks = split(pattern, string [, limit ]); flags ]]);
The pattern matches the text that separates the
chunks. For instance, to split out the terms from The pattern matches a separator between two
an arithmetic expression: chunks. By default, the separators are not
returned.
$expression = '3*5+i/6-12';
The optional limit specifies the maximum
$terms = split('[/+*-]', $expression); number of chunks to return (-1 is the default,
// $terms is array('3', '5', 'i', '6', '12) which means all chunks).
The flags argument is a bitwise OR combination
of the flags PREG_SPLIT_NO_EMPTY and
PREG_SPLIT_DELIM_CAPTUR.

For example, to extract just the operands from a


simple numeric expression, use:
$ops = preg_split('{[+*/-]}', '3+5*9/2');

// $ops is array('3', '5', '9', '2') .

To extract the operands and the operators, use:

$ops = preg_split('{([+*/-])}', '3+5*9/2', -1,


PREG_SPLIT_DELIM_CAPTURE);

// $ops is array('3', '+', '5', '*', '9', '/', '2')

You might also like