通过PHP自带的服务器来查看正则匹配结果的方法_php技巧
众所周知,PHP代码需要web服务器来执行,要测试PHP代码就得搭建一个web服务器,这就给我们平时学习带来了较多不便。不过好在PHP v5.4版本以后,PHP会自带一个功能简单的web服务器。
启动内置web服务器
首先,进入自定义的web文件夹,然后启动内置web服务器:
cd ~/public_html php -S localhost:8000
端口号8000是自定义的,换成其他未使用端口均可。
启动后,控制界面如下所示:
测试内置服务器
在public_html文件夹下建立test.php,
<?php phpinfo(); ?>
然后在浏览器中访问localhost:8000/test.php,应该就可以看到php的信息页面:
正则匹配
我们来看下PHP进行正则匹配的一个简单例子:
<?php $subject = 'abc3def'; $pattern = '/c\dde/'; preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE); print_r($matches); ?>
可以通过PHP的内置web服务器来查看运行结果,不出意外的话你可以看到如下输出,
Array ( [0] => Array ( [0] => c3de [1] => 2 ) )
接下来我们来仔细分析这个代码。
preg_match函数
preg_match函数的原型是int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )。其中pattern即为正则表达式,subject是被匹配的字符串,后面的都是可选参数。代码中的PREG_OFFSET_CAPTURE即为flags值,作用就是将匹配结果的偏移量同匹配结果一起输出至matches变量中,默认只将匹配结果输出至matches中。关于该函数的详细说明,请参看preg_match官方文档。
正则表达式
代码中的‘/c\def/'即为正则表达式,在大多数程序中,正则表达式都被置于两个正斜杠之间。d表示匹配数字,因此代码中的正则表达式匹配的是c数字def的字符串。关于正则表达式的更多语法,可以参看正则表达式30分钟入门教程。这里顺便提一点,正则表达式的第二个斜杠之后可以添加一个模式修饰符。最简单的模式修饰符就是i,匹配时忽略大小写。例如,正则表达式/def/匹配字符串abcDef会失败,而/def/i匹配字符串abcDef则会成功。更多的模式修饰符可参看模式修饰符。
print_r函数
print_r函数打印一个变量易于理解的信息。不同于print和echo只能打印字符串、整型等普通变量,print_r还可以打印array变量以及object变量,并以易于理解的格式输出。讲到这再延伸讲下,PHP中还有一个经常会用到的打印信息的函数,就是var_dump函数。正如函数的名称一样,这个函数经常在调试下使用,除了能打印变量的值,还能打印变量的类型。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
