In this script the main ereg function is not used in an if statement. It assumes the
browser will identify itself minimally as a name, a slash, and the version. The match
array gets set with the parts of the evaluated string that match with the parts of the regular
expression. There are three subexpressions for name, version, and any extra description.
Most browsers follow this form, including Navigator and Internet Explorer. Since
Internet Explorer always reports that it is a Mozilla (Netscape) browser, extra steps must
be taken to determine if a browser is really a Netscape browser or an imposter. This is
done with a call to eregi.
If you are wondering why element zero is ignored, that's because the zero element holds
the substring that matches the entire regular expression. In this situation it is not
interesting. Usually the zero element is useful when you are searching for a particular
string in a larger context. For example, you may be scanning the body of a Web page for
URLs. Listing 16.4 fetches the PHP home page and lists all the links on the page.
The main loop of this script gets lines of text from the file stream and looks for HREF
properties. If one is found in a line, it will be placed in the zero element of the match
array. The script prints it out and then removes it from the line using the
ereg_replace function. This function replaces text matched with a regular expression
with a string. In this case the script replaces the HREF property with an empty string. The
reason for finding the link and then removing it is that it is possible for two links to be on
one line of HTML. The ereg function will match the first substring only. The solution is
to find and remove each link until none remain.
Notice that when removing the link a replace variable is prepared. Some links might
contain a question mark, a valid character in a URL that separates a filename from form
variables. Since this character has special meaning to regular expressions, the script
places a backslash before it to let PHP know it's to be taken literally.
I frequently use ereg_replace to convert text for use in a new context. You can use
ereg_replace for replacement of end-of-line characters with break tags. Listing
16.5 demonstrates this idea. You can also use it to collapse multiple spaces with a single
space.
Listing 16.4 Scanning Text for URLs