How to view the regular matching results with the server from PHP _php tips

Source: Internet
Author: User
Tags modifier

As we all know, PHP code needs a Web server to execute, to test the PHP code will have to build a Web server, which gives us a lot of inconvenience in peacetime learning. But after the PHP v5.4 version, PHP will bring a simple Web server.

Start the built-in Web server

First, go to your customized Web folder, and then start the built-in Web server:

CD ~/public_html
php-s localhost:8000

The port number 8000 is custom and is available for use in other unused ports.

After startup, the control interface looks like this:

Testing the built-in server

Create a test.php under the Public_html folder,

<?php
phpinfo ();
? >

Then in the browser to access localhost:8000/test.php, you should be able to see the information page of PHP:

Regular match
let's look at a simple example of a regular match in PHP:

<?php
$subject = ' abc3def ';
$pattern = '/c\dde/';
Preg_match ($pattern, $subject, $matches, preg_offset_capture);
Print_r ($matches);
? >

You can view the results of the operation through PHP's built-in Web server, and you can see the following output without any surprises,


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

Next, we'll look at 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]]). Where pattern is the regular expression, subject is the matched string, followed by optional parameters. The preg_offset_capture in the code is the flags value, which is to output the matching result to the matches variable with the matching result, by default only the matching result is output to the matches. For a detailed description of this function, refer to the Preg_match official documentation.

Regular expressions

The '/c\def/' in the code is a regular expression, and in most programs the regular expression is placed between two forward slashes. \d represents a matching number, so the regular expression in the code matches the string of C-digit def. For more syntax on regular expressions, refer to the 30-minute introductory tutorial for regular expressions. Here, incidentally, a pattern modifier can be added after the second slash of a regular expression. The simplest pattern modifier is I, which ignores case when matched. For example, the regular expression/def/match string abcdef fails, and/def/i matching string abcdef succeeds. More pattern modifiers are available in the pattern modifier.

Print_r function

The Print_r function prints information that is easy to understand for a variable. Unlike print and Echo, which can print only ordinary variables such as strings, integers, Print_r can also print array variables and object variables and output in an Easy-to-understand format. Speaking of this extension, PHP also has a regular use of the print information function, is the Var_dump function. As with the name of the function, this function is often used under debugging, in addition to being able to print the value of a variable, but also to print the type of the variable.

Related Article

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.