<<

. 114
( 132 .)



>>

{
echo $array[1];
}

Because the question-mark character means “zero or one of the previous charac-
ter,” the pairing [[:space:]]? means that one whitespace character can exist, or
none. If you want to allow for more than one whitespace character, you can use
[[:space:]]*.
Finally, you need to deal with the actual contents of the href attribute. So far
you™ve accounted only for cases in which the link destination is delimited by dou-
ble quotes. But at the very least you should account for delimiters of either double
quotes or single quotes. To do that you need to put double quotes and single quotes
within a character class. Because you™ve surrounded the entire regular expression with
single quotes, you need to escape single quotes within the regular expression with
backslashes. The class will be [“\™].

if (eregi(˜<a.*href[[:space:]]?=[[:space:]]?[“\™](.*)[“\™]™,
$anchor, $array))
{
echo $array[1];
}

To be even more complete, the regular expression should account for cases in
which no quotation mark at all is used to delimit the value of the href. For exam-
ple, browsers are just fine with a tag like this: <a href=mylink.php>. In a case like
this it might be a good idea to add a question mark character after the [“\™] class
that marks the beginning of the href, and to include [:space:] and the greater-
than sign (>) to mark the end of the href. All you need to do is add them to the last
character class:


if (
Appendix G: Regular Expressions Overview 667

eregi(
˜<a.*href[[:space:]]?=[[:space:]]?[“\™]?(.*)[“\™>[:space:]]™
, $anchor
, $array
)
)
{
echo $array[1];
}

However, this arrangement presents some problems that you may not have
anticipated. Imagine that the preceding code is attempting to match this string: <a
href = ../my_link.php>this is my link text</a>. When you add the greater-
than sign to the character class, the regular expression does not match the first
greater-than sign in the string ” it matches the final greater-than sign. This situa-
tion is known as greedy matching, and you can™t get around it using ereg() or
ereg_replace().
If you need to match the first occasion of a character in a string you need to
make use of the PCRE functions. With PCRE, the combination .*? matches all char-
acters until the first occasion of the character you indicate. The following series
matches everything until the first double quote, single quote, or greater-than sign:

.*?[“\™>]

With preg_match() the final function looks like this:

if
(preg_match(˜/<a.*?href[[:space:]]?=[[:space:]]?[“\™]?(.*?)[“\™>]/i™
,
$anchor, $array))
{
echo $array[1];
}
Appendix H

Helpful User-Defined
Functions
THIS a series of PHP functions and classes that you might find
APPENDIX CONTAINS
useful in creating your scripts. It starts with a run-through of the base functions
kept in the /book/functions folder.



Base Functions Set
Used in This Book
We discuss these in detail in Chapter 8, but we include them here for quick reference.

From functions/basic/
These functions deal with authentication and text manipulation.

authenticate()
This function gets a username and password, either from the browser by sending a
401 header or from a regular Web page, and verifies the results against a database
table named admin.

void authenticate([string realm [, string message]])



cleanup_text()
This function removes HTML and PHP tags using the strip_tags() function and
replaces <, >, &, and “ characters with their HTML entities. If the second argument
is not empty strip_tags will not be run, and only the HTML-entity replacement
will occur. The third argument can specify tags that should not be removed.

string cleanup_text ([string value [, string preserve [, string
allowed_tags]]])




669
670 Part V: Appendixes

make_page_title()
This function cleans up a string to make it suitable for use as the value of an HTML
<TITLE> tag, removing any HTML tags and replacing all HTML entities with their
literal-character equivalents by using get_html_translation_table (HTML_
ENTITIES).

string make_page_title ([string title])

money()
This function formats the sole argument as a standard U.S. dollar value, rounding
any decimal value to two decimal places for cents and prepending a dollar sign to
the returned string. Commas serve as thousands separators. However, remember that
you can run into rounding errors with numbers of type float. This is only an issue in
complex applications, though.

string money ([mixed value])

states()
This function returns an associative array, the key being the two-letter abbreviation
of the states, the value being the state name.

array states(void)


From functions/database/
These are common functions that help you work with MySQL databases.

db_connect()
This function creates a database connection using the PEAR DB class. It accepts a
variable list of parameters. The most common one used in the examples in this
book is application, which is used as an index into values retrieved from an .ini
file (typically stored outside the document root of the Web server).

void db_connect ()

db_fetch_record()
This function selects values from the MySQL table specified by the first argument.
If the optional second and third arguments are not empty, the select gets the row
from that table in which the column named in the second argument has the value
given by the third argument. The second and third arguments can also be arrays, in
which case the query builds its where clause using the values of the second array of
arguments as the table-column names and the corresponding values of the third
array of a arguments as the required values for those table columns.

db_fetch_record(string table [,string key [,string value]])
Appendix H: Helpful User-Defined Functions 671

db_values_array()
This function builds an associative array out of the values in the MySQL table spec-
ified in the first argument. The data from the column named in the second argu-
ment are set to the keys of the array. If the third argument is not empty, the data
from the column it names are the values of the array; otherwise, the values are
equal to the keys. If the third argument is not empty, the data are ordered by the
column that is named; otherwise, they are ordered by the key column. The optional
fourth argument specifies any additional qualification for the query against the
database table; if it is empty, all rows in the table are retrieved.
If either the first or second argument is empty, no query is run and an empty
array is returned. The function presumes that whoever calls it knows what he or she
is doing ” that the table exists, that all the column names are correct, and so on.

array db_values_array ([string table name [, string value field [,
string label field [, string sort field [, string where clause]]]]])


From functions/html/
These functions create common HTML elements, including anchors and unordered
lists.

font_tag()
This function creates an HTML font tag. Default size is 2; default font face is sans-
serif. Any additional attributes in the third argument are added to the tag. It is
expecting an associative array, the key of which is the name of the attribute; the
value of the array element is the attribute value.

string font_tag ([int size [, string typeface [, array attributes]]])

anchor_tag()
This function creates an HTML anchor tag. The first argument is the href value; the
second the string to be surrounded by the anchor. It is expecting an associative
array, the key of which is the name of the attribute; the value of the array element
is the attribute value.

string anchor_tag ([string href [, string text [, array attributes]]])

image_tag()
This function returns an HTML image tag (<img>). The first argument gives the URL
of the image to be displayed. Additional attributes can be supplied as an array in
the third argument.

string image_tag ([string src [,array attributes]])
672 Part V: Appendixes

subtitle()
This function returns an HTML <h3> tag. It is used for the titles of secondary areas
within pages in our examples. The reason to display these via a function rather
than just literal <h3> tags is that this will enable you to change the format of these
subtitles in one place, instead of in each script.

string subtitle(string string)

paragraph()
This function returns a string inside HTML paragraph (<p>) tags. Attributes for the
<p> tag can be supplied in the first argument. Any additional arguments are
included inside the opening and closing <p> tags, separated by newlines.

string paragraph([array attributes [, mixed ...]])

ul_list()
This function returns an HTML unordered (bulleted) list (<ul> tags). If the argument
is an array, each value from the array is included as a list item (<li>) in the list.
Otherwise, the argument is simply included inside the <ul> tags as is.

string ul_list(mixed values)

The following functions create opening and closing <table> tags, as well as
<tr> and <td> tags.

start_table()
This function returns an opening HTML table tag inside a pair of paragraph
(<p>...</p>) tags. Attributes for the table can be supplied as an array.

string start_table([array attributes])

end_table()
This function returns a closing table tag.

string end_table(void)

table_row()
This function returns a pair of HTML table row (<tr>) tags enclosing a variable
number of table cell (<td>) tags. If any of the arguments to the function is an array,
it is uses as attributes for the <tr> tag. All other arguments are used as values for the
cells of the row. If an argument begins with a <td> tag, the argument is added to the
Appendix H: Helpful User-Defined Functions 673

row as is; otherwise, it is passed to the table_cell() function and the resulting
string is added to the row.

string table_row ([array attributes], [indefinite number of string
arguments])

table_cell()
This function returns an HTML table cell (<td>) tag. The first argument is used as
the value of the tag. Attributes for the <td> tag are supplied as an array in the sec-
ond argument. By default the table cell is aligned left horizontally and to the top
vertically.

string table_cell ([string value [, array attributes]])


From functions/forms/
These functions create all common form elements, as well as the opening and clos-
ing <form> tags.

start_form()
This function returns an HTML <form> tag. If the first argument is empty, the value
of the global Apache variable SCRIPT_NAME is used for the action attribute of the
<form> tag. Other attributes for the form can be specified in the optional second
argument; the default method of the form is post. The behavior of this function on
servers other than Apache has not been tested. It™s likely that it will work, because
SCRIPT_NAME is part of the CGI 1.1 specification.

<<

. 114
( 132 .)



>>