The application of regular expression in Network programming (3)

Source: Internet
Author: User
Tags ereg implement php language parse string php code regular expression split valid
Programming | network | Regular application Example

With a more comprehensive understanding of regular expressions, you can use regular expressions in perl,php, as well as in programs such as ASP.

The following is an example of the PHP language, using the verify that the e-mail address entered by the user online and the URL is in the correct format. PHP provides a eregi () or ereg () data processing function to implement string alignment profiling of pattern matching operations the use format of the Ereg () function is as follows:

Ereg (pattern, string)

Where pattern represents the schema of the regular expression, and string is the target object, such as an email address value, that performs a find-and-replace operation. This function is used to parse string strings based on pattern rules, and the return value is true if found. The difference between a function ereg () and a eregi () is that the former is case-sensitive and the latter has nothing to do with case. The program code written using PHP is as follows:

<?php
if (Ereg ("^ ([a-z0-9_-]) +@" ([a-zz0-9_-]) + (\.[ a-z0-9_-]) +[a-z]{2,3}$ ", $email))
{echo "Your e-mail through the preliminary check!" ";}
Else
{echo "is not a valid e-mail address, please re-enter!") ";}
? >


This example is a simple check for user-entered e-mail. Check whether the user's e-mail string has @ characters, in the @ character before the lowercase English letters, numbers or "_", after the @ There are several sections of string, the last decimal point can only have two or three lowercase English letters. such as Webmaster@mail.sever.net, hello_2001@88new.cn can be checked, while New99@253.com (uppercase letters) and New99@253.comn (the last decimal point can only be more than 3 English letters) Can not pass the examination.

We can also check operations by invoking custom positive rule discriminant functions, such as the following URL validation function:

function Verifywebsiteaddr ($strWebSiteAddr) {
Return (eregi ("^" [_0-9a-z-]+.) + ([0-9a-z-]+.) +[a-z]{2,3}$ ", $strWebSiteAddr));
}

We know that PHP programs must run with server support, and embedded scripting language JavaScript may be a good choice if you want to implement the above features on your home page. JavaScript comes with a powerful regexp () object that can be used to match regular expressions. The test () method can verify that the target object contains a match pattern and returns TRUE or false accordingly. You only need to add a section of JavaScript code to the
Language= "Javascript1.2"
function verifyaddress (obj) {
var email = obj.email.value;
var pattern =/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + (\.[ A-za-z0-9_-]) +/;
Flag = pattern.test (email);
if (flag) {
Alert ("Your e-mail through preliminary check!") ");
return true;}
else{
Alert ("Not a valid e-mail address, please re-enter!") ");
return false;}
}
</script>


Then in the form field that you enter information in the Web page, add one line to the <form> label area with the following code:

<onsubmit= "return verifyaddress (this);" >

When the submit button is pressed, the verifyaddress () function is run first, the match is identified, and if the condition is met, the form information is sent to the target page, otherwise the error message is returned.

In fact, the function of regular expressions is far from the one mentioned in this article, and next time, we'll introduce a technique for using regular expressions to extract any kind of textual information from any given Web page, such as all the picture filenames in a Web page.


Extraction of image tags in HTML source files

In the last chapter, we introduce the concept of regular expression and the application of regular expression in network programming to verify the user's email address entered online and the format of the URL is correct. Today we introduce a programming technique to extract the image label from a specified Web page source file, that is, from a Web page source file, Parse out all the illustration file names (including the picture path), which is the name of the label http://www.163design.net/a/z/ "... abc.jpg" (some may be in GIF format). Programming Environment: Php+apache for Win98.

First, create a new PHP-type file with a text editor: Abstractsrcfrompage.php3. For ease of explanation, we intend to enter the URL (or native document) of the Web page that needs to extract the image label in the browser form field, and then perform the disjunction after the submission, so in that file we will create a form for entering the URL, for example:


<form action= "abstractsrcfrompage.php3" method= "POST"
Enter the URL <input type=text name=filename> <br>
<input type=submit name=submit value= "Submit"
</form>


Input the correct URL, after submitting the form information is sent to the ABSTRACTSRCFROMPAGE.PHP3 page, because the form itself is on the page, so the equivalent of being sent to its own page, we need to write the extraction processing of the PHP code, followed by the form code snippet after writing the following code:


<?php
if ($filename!= "") {
$fp = fopen ($filename, "R"); file://if the input is not empty, open local or remote files;
while ($buffer = fgets ($fp, 1024)) {
$source. = $buffer; }
Fclose ($FP);
file://Find out if there is a Such a mark
if (eregi (]+ (src=\ ") +[^\*\" <> | +(\.) + (GIF) | (jpg)) + (\ ")", $source)) {
echo "Find picture label:) <br>";}
else{
Echo did not find the picture label: (<br>;}
file://Split, the first time with the label, $splitres =split ((\ ">) | ()) + (]+ (src=\ ")", $source);
Echo Found: $imagenums-1 Pictures <br> respectively: <br>;
for ($i =1; $i <sizeof ($splitres); $i + +) {
file://two times split, with "split." Because the filename can contain ", the first element of the resulting split group is the path + filename;
Unset ($imgname); Delete Imgname variables before using again;
$imgname =spliti ("\", $splitres [$i])//To assign the extraction picture information to the imgname variable in turn
echo "$i =>". $imgname [0]. " <br> "; Picture information for file://output extraction
}
}
? >


The design idea of this procedure is: PHP program to determine whether to enter the file name (URL or local file name), if not NULL to open the file as read-only; then use function fgets (fp,length) to obtain the line that the file index FP refers to and return the line within the length of length-1 String, in the example above is 1024-1=1023, and then use the string comparison to parse the function ereg () to find out if $source contains
where the function sizeof ($splitres) returns the number, in the For loop, sequentially assigns each element of the array splitres (also an array) to the array variable imgname, and outputs the first element value of imgname (for a graphical path and filename found). When the next loop is executed, the variable imgname is deleted for reuse purposes. We can look into the mysteries.

OK, after writing, save the ABSTRACTSRCFROMPAGE.PHP3 to your server designated directory, start the Apache server, open it in the browser, casually enter a Web page name or remote URL to see how the effect.

If you are interested, you can try to extract any interesting information in the HTML document, if a little modification, do a website text search engine is not more wonderful?



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.