_php tutorial for regular functions in PHP

Source: Internet
Author: User
Tags ereg php regular expression posix preg
PHP regular expressions are primarily used for pattern segmentation, matching, finding, and substitution of strings. Using regular expressions can be inefficient in some simple environments, so how to better use PHP regular expressions requires a combination of considerations.

My PHP is getting started, is the origin of an article on the Internet, this article is based on the use of PHP regular expression method, I think is a very good entry material, but the study still depends on the individual, in the process of use, or will continue to forget, so repeatedly read this article has four or five times, For some of the more difficult points of knowledge, and even a long time to digest, but as long as you can see the adherence to the reading, you will find that their ability to use the regular will be significantly improved.

The definition of the PHP regular expression:

A syntax rule that describes the pattern of character arrangement and matching. It is used primarily for pattern segmentation, matching, finding, and substitution of strings.

Regular functions in PHP:

There are two regular functions in PHP, each of which has the same function:

The set is provided by the Pcre (Perl Compatible Regular Expression) library. A function named with the prefix "Preg_";

A set of extensions provided by the POSIX (portable Operating System Interface of Unix) extension. Use a function named "Ereg_" as a prefix (POSIX regular library, since PHP 5.3, is not recommended, after PHP6, will be removed)

Since POSIX is about to roll out the historical stage, and Pcre and Perl are similar in form, it's better for us to switch between Perl and PHP, so here's a focus on the use of pcre.

Pcre Regular Expressions

Pcre is all called Perl Compatible Regular expression, which means Perl-compatible regular expressions.

In Pcre, a pattern expression (that is, a regular expression) is typically included between two backslashes "/", such as "/apple/".

Some of the important concepts in the regular are: metacharacters, escapes, pattern units (repetitions), antisense, references, and assertions, all of which can be easily understood and mastered in article [1].

Common metacharacters (Meta-character):

Metacharacters description

A matches the atom of the string header

Z matches the atom at the end of a string

b matches the boundary of the word/bis/match header is the string of is/isb/matches the string of the end is the/bisb/bound

B matches any character except the word boundary/bis/matches the "is" in the word "this"

D matches a number; equivalent to [0-9]

D matches any character except a number; equivalent to [^0-9]

W matches an English letter, number, or underscore; equivalent to [0-9a-za-z_]

W matches any character except English letters, numbers, and underscores; equivalent to [^0-9a-za-z_]

s matches a white space character; equivalent to [FTV]

S matches any character except white space characters; equivalent to [^FTV]

F matches a page break equivalent to x0c or CL

Match a line break; equivalent to x0a or CJ

Match a return character equivalent to x0d or CM

t matches a tab; equivalent to x09 or CL

V matches a vertical tab; equivalent to x0b or CK

Onn matches an octal number

XNN matches a hexadecimal digit

CC matches a control character

Pattern modifier (Pattern Modifiers):

Pattern modifiers are particularly used in ignoring case, matching multiple lines, and mastering this modifier can often solve many of the problems we encounter.

I-can match uppercase and lowercase letters

M-Treats a string as multiple lines

S-Treats the string as a single line, and the newline character is treated as ordinary characters, so that "." Match any character

X-whitespace in the pattern is ignored

U-Match to the nearest string

E-Use the replaced string as an expression

Format:/apple/i matches "Apple" or "apple" and so on, ignoring case. /I

Pcre Mode Unit:

1 extracting attributes of first bit

/^d{2} ([W]) d{2}1d{4}$ matches strings such as "12-31-2006", "09/27/1996", "86 01 4321". However, the above regular expression does not match the format of "12/34-5678". This is because the result "/" of the Pattern "[W]" has been stored. When the next position "1" is referenced, its matching pattern is also the character "/".

Use non-storage mode unit "(?:)" When you do not need to store matching results

For example/(?: A|b|c) (d| e| F) 1g/will match "AEEg". In some regular expressions, it is necessary to use a non-storage mode unit. Otherwise, the order of subsequent references needs to be changed. The above example can also be written/(A|B|C) (c| e| F) 2g/.

Pcre Regular Expression functions:

Preg_match () and Preg_match_all ()

Preg_quote ()

Preg_split ()

Preg_grep ()

Preg_replace ()

Function of the specific use, we can be found through the PHP manual, the following share some of the usual accumulation of regular expressions:

Match Action Property

$str = ";

$match = ";

Preg_match_all ('/s+action= ' (?! http:) (. *?) " S/', $str, $match);

Print_r ($match);

Using callback functions in regular

/**

* Replace some string by callback function

*

*/

function Callback_replace () {

$url = ' http://esfang.house.sina.com.cn ';

$str = ";

$str = Preg_replace ('/(? <=saction= ") (?! http:) (. *?) (? = "s)/E ', ' Search ($url, 1) ', $str);

Echo $str;

}

function Search ($url, $match) {

Return $url. '/' . $match;

}

Regular matching with assertions

$match = ";

$str = ' xxxxxx.com.cn bold font

Paragraph text

';

Preg_match_all ('/(?<=< (W{1}) >). * (? = )/', $STR, $match);

echo "matches the contents of an HTML tag without attributes:";

Print_r ($match);

Replace the address in the HTML source

$form _html = Preg_replace ('/(? <=saction= "|ssrc=" |shref= ") (?! Http:|javascript) (. *?) (? = "s)/E ', ' Add_url ($url, ' 1 ') ', $form _html);

Finally, while the regular tools are powerful, in terms of efficiency and writing time, it may not be possible to explode more directly, and for some urgent or demanding tasks, a simple, crude approach may be better.

And for the Preg and Ereg two series between the execution efficiency, have seen the article said preg to be faster, specifically because the use of ereg is not much, but also to launch the history stage, and then add all people prefer pcre way, so I do not compare, familiar friends can make the following comments, Thank you.

http://www.bkjia.com/PHPjc/371399.html www.bkjia.com true http://www.bkjia.com/PHPjc/371399.html techarticle PHP Regular expressions are primarily used for pattern segmentation, matching, finding, and substitution of strings. Using regular expressions can be inefficient in some simple environments, so how to better make ...

  • 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.