Regular expression (php function), regularexpression_php tutorial

Source: Internet
Author: User

Regular expression (php function), regularexpression


1. Regular expressions are a tool for searching and matching strings

2. Regular expression functions commonly used in PHP

    • Preg_match ($pattern, $subject)
    • Preg_match_all ($pattern, $subject, array & $matches)
    • Preg_replace ($pattern, $replacement, $subject)
    • Preg_filter ($pattern, $replacement, $subject)
    • Preg_grep ($pattern, array $input)
    • Preg_split ($pattern, $subject)
    • Preg_quote ($STR)

3. Function description

$pattern = Regular Expression

$subject = target function to match

(1) Preg_match () and Preg_match_all (): Return matches the number of results

    • Preg_match ($pattern, $subject, [array & $matches]): Match only once with a result of 0 or 1, the third argument is not writable, the third parameter represents a reference to the address
    • Preg_match ($pattern, $subject, Array & $matches): match all, result 0,1,2 ...

eg

$pattern = '/[0-9]/';

$subject = ' Weuyr3ui76as83s0ck9 ';

$m 1 = $m 2 = array ();

T1 = Grep_match ($pattern, $subject, $m 1);

T2 = Grep_match_all ($pattern, $subject, $m 2);

Result: M1 = array ([0]=>3)

m2 = Array ([0]=>array ([0]=>3,[1]=>7,[2]=>6,[3]=>8,[4]=>3,[5]=>0,[6]=>9))

T1 = 1

T2 = 7

(2) Preg_replace and Preg_filter: support for array substitution

    • Preg_replace ($pattern, $replacement, $subject): Retains values that have been replaced and have not occurred
    • Preg_filter ($pattern, $replacement, $subject): Retains the value of the substitution that occurred

eg one:

$pattern = '/[0-9]/';

$subject = ' Weuyr3ui76as83s0ck9 ';

$replacement = ' surplus ';

$str 1 = preg_replace ($pattern, $replacement, $subject);

$str 2 = Preg_filter ($pattern, $replacement, $subject);

Results:

$str 1 = ' Weuyr-ying UI yingying as Yingying S Ying ck ying '

$str 2 = ' Weuyr-ying UI yingying as Yingying S Ying ck ying '

Eg:

$pattern = Array ('/[0123]/', '/[456]/', '/[789]/')

$replacement = Array (' Ah ', ' la ', ' winded ')

Results:

$str 1 = ' Weuyr Ah, the UI is winded as a, s ah ck '

$str 2 = ' Weuyr Ah, the UI is winded as a, s ah ck '

EG three:

$subject = Array (' Weuy ', ' r3ui ', ' 76as83 ', ' s ', ' 0ck9 ');

Results:

$str 1 = Array ([0]=>weuy, [1]=>r] UI, [2]=>, [3]=>s, [4]=>, CK)

$str 2 = Array ([1]=>r UI, [2]=>, [4]=>, CK)

(3) Grep_grep ($pattern, array $input): Castrated version of Grep_filter (), do only match, do not replace

eg

$pattern = '/[0-9]/';

$subject = Array (' Weuy ', ' r3ui ', ' 76as83 ', ' s ', ' 0ck9 ');

$arr = Preg_grep ($pattern, $subject);

Results:

$arr = Array ([1]=>r3ui, [2]=>76as83, [4]=>0ck9)

(4) Grep_split ($pattern, $subject): Explode is a subset of the function

eg

$pattern = '/[0-9]/';

$subject = ' You 2 good 3 Ah! '

$arr = Preg_split ($pattern, $subject);

Results:

$arr = ([0]=> you, [1]=> good, [2]=> Ah!)

  

http://www.bkjia.com/PHPjc/1109850.html www.bkjia.com true http://www.bkjia.com/PHPjc/1109850.html techarticle Regular Expression (php function), regularexpression 1. Regular expressions are a tool for string search and matching 2. Common regular expression functions in PHP Preg_match ($pattern, $subject )...

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