Use the PHP server to view the regular expression matching result.

Source: Internet
Author: User
Tags php server php regular expression
This article mainly introduces how to view the regular expression matching result through the PHP built-in server. this method can be used to complete a lot of lightweight experiments in the work environment before moving on to the server, as we all know, if you need PHP code, you need a web server to run it. to test the PHP code, you have to build a web server, which makes it inconvenient for us to study at ordinary times. However, after PHP v5.4, PHP will provide a simple web server.

Start the built-in web server

First, go to the custom web folder and start the built-in web server:

cd ~/public_htmlphp -S localhost:8000

The port number 8000 is customized and can be changed to other unused ports.

After the startup, the control interface is shown as follows:

Test the built-in server

Create test. php in the public_html folder,

<?phpphpinfo();?>

Then access localhost: 8000/test. php in the browser and you will be able to see the php information page:

Regular Expression Matching
Let's take a simple example of PHP regular expression matching:

<?php$subject = 'abc3def';$pattern = '/c\dde/';preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);print_r($matches);?>

You can view the running result through the built-in web server of PHP. you can see the following output without exception,


Array(  [0] => Array  (    [0] => c3de    [1] => 2  ))

Next, we will analyze the code carefully.

Preg_match function

The prototype of the preg_match function is int preg_match (string $ pattern, string $ subject [, array & $ matches [, int $ flags = 0 [, int $ offset = 0]). Here, pattern is a regular expression, and subject is a matched string, followed by optional parameters. In the code, PREG_OFFSET_CAPTURE is the flags value, which is used to output the offset of the matching result together with the matching result to the matches variable. by default, only the matching result is output to matches. For more information about this function, see the official preg_match documentation.

Regular expression

'/C \ def/' in the code is a regular expression. in most programs, regular expressions are placed between two forward slashes. D indicates matching numbers. Therefore, the regular expression in the code matches the string of the c-digit def. For more information about the regular expression syntax, see the regular expression 30-minute getting started tutorial. By the way, you can add a pattern modifier after the second slash of the regular expression. The simplest pattern modifier is I. case sensitivity is ignored during matching. For example, the regular expression/def/matches the string abcDef and the/def/I matches the string abcDef. For more pattern modifiers, see pattern modifiers.

Print_r function

The print_r function prints a variable that is easy to understand. Unlike print and echo, print_r can only print string, integer, and other common variables. It can also print array and object variables and output them in an easy-to-understand format. To put it further, there is also a frequently used function for printing information in PHP, namely the var_dump function. Just like the name of a function, this function is often used for debugging. in addition to printing the value of a variable, it can also print the type of a variable.

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.