Simple analysis of PHP regular expression function library

Source: Internet
Author: User
Tags ereg expression php regular expression posix regular expression resource

This article introduces you to the PHP regular Expression function library (POSIX extension) greedy matching, and we want to be aware of the attributes that are not supported in conditional child mode and many other POSIX extended regular expression syntax.

PHP Regular Expression function library learning warnings:

This class of regular expressions is not safe for binary mode. The PCRE function library can be. Regular expressions are used in PHP to do complex string operations. Functions that support regular expressions are:

Ereg ()

Ereg_replace ()

Eregi ()

Eregi_replace ()

Split ()

Spliti ()

These functions accept a regular expression string as its first argument. PHP uses POSIX-extended regular expressions defined by POSIX 1003.3. The full description of POSIX regular expressions is shown in the man page contained in the Regex directory in the PHP release package. Can be read by a similar man/usr/local/src/regex/regex.7 command.

Requirements: Support for this extension module is available without external library files.

Install Warning: Do not change TYPE unless you know what you are doing.

To activate RegExp support, add--with-regex[=type when configuring PHP. TYPE can be one of System,apache or PHP. PHP is used by default.

The Windows version of PHP has built-in support for the extension module. You do not need to load any additional extensions to use these functions. Let's look at the specific operations:

Runtime configuration: The extension module does not define any settings directives.

Resource type: The extension module does not define any resource types.

Predefined constants: The extension module does not define any constants.

Examples of PHP regular expression functions:

<?php
// 如果在 $string 中任何地方找到 "abc" 则返回 &true;
ereg ("abc", $string);

// 如果 $string 以 "abc" 开头则返回 &true;
ereg ("^abc", $string);

// 如果 $string 以 "abc" 结尾则返回 &true;
ereg ("abc$", $string);

// 如果用户浏览器是Netscape 2,3或MSIE 3则返回&true;
eregi ("(ozilla.[23]|MSIE.3)", $HTTP_USER_AGENT);

// 将三个空格分隔的单词放入$regs[1],$regs[2]和$regs[3]中
ereg ("([[:alnum:]]+) ([[:alnum:]]+)
([[:alnum:]]+)", $string,$regs);

// 将<br />标记放到 $string 开头
$string = ereg_replace ("^", "<br />", $string);

// 将<br />标记放到 $string 结尾
$string = ereg_replace ("$", "<br />", $string);

// 删除$string中的所有换行符  
$string = ereg_replace ("\n", "", $string);
?>

PHP Regular expression functions related to the introduction to you here, I hope you understand and learn the PHP regular expression function is helpful.

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.