A way to view the results of a regular match from a server that comes with PHP,
As we all know, PHP code needs a Web server to execute, to test the PHP code to build a Web server, which gives us a lot of inconvenience of learning. However, after PHP v5.4 version, PHP comes with a simple Web server.
Start the built-in Web server
First, go to your custom Web folder and start the built-in Web server:
CD ~/public_htmlphp-s localhost:8000
Port number 8000 is custom and can be replaced with other unused ports.
After startup, the control interface looks like this:
Test the built-in server
Set up test.php under the Public_html folder,
<?phpphpinfo ();? >
Then access the localhost:8000/test.php in the browser, you should be able to see the PHP Information page:
Regular match
Let's look at a simple example of a regular match for PHP:
<?php$subject = ' abc3def '; $pattern = '/c\dde/';p reg_match ($pattern, $subject, $matches, preg_offset_capture);p rint _r ($matches);? >
You can see the results of the operation through PHP's built-in Web server, but you can see the output as follows.
Array ( [0] = = Array ( [0] = = C3de [1] = 2 ))
Next, let's examine the code carefully.
Preg_match function
The prototype of the Preg_match function is an int preg_match (string $pattern, String $subject [, Array & $matches [, int $flags = 0 [, int $offset = 0]]). Where pattern is a regular expression, subject is the string that is matched, followed by an optional argument. The preg_offset_capture in the code is the flags value, which is the function of outputting the offset of the matching result to the matches variable with the matching result, and the default is to output only the matching result to matches. For a detailed description of the function, see the official Preg_match 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. The \d represents a matching number, so the regular expression in the code matches the C-digit def string. For more syntax on regular expressions, refer to the regular expression 30-minute introductory tutorial. Here, incidentally, a pattern modifier can be added after the second slash of the 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 can be found in the pattern modifier.
Print_r function
The Print_r function prints a variable easily understandable information. Unlike print and Echo, which only prints common variables such as strings, integers, Print_r can also print array variables and object variables, and output them in an easy-to-understand format. Speaking of this extension, PHP also has a frequently used printing information function, is the Var_dump function. Like the name of a function, this function is often used for debugging purposes, in addition to the ability to print the value of a variable and to print the type of the variable.
Articles you may be interested in:
- Most commonly used PHP regular expression collection and collation
- A detailed description of the operating modes of PHP based on various web servers
http://www.bkjia.com/PHPjc/1084541.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084541.html techarticle With PHP's own server to view the regular matching results, it is well known that the PHP code needs a Web server to execute, to test the PHP code to build a Web server, which gives me ...