Home > Backend Development > PHP Tutorial > regular expression (php function), regularexpression_PHP tutorial

regular expression (php function), regularexpression_PHP tutorial

WBOY
Release: 2016-07-12 08:57:14
Original
1027 people have browsed it

regular expression (php function), regularexpression

1. Regular expression is a tool for string search and matching

2. Commonly used regular expression functions 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 = matching objective function

 (1) preg_match() and preg_match_all(): return the number of times the result was matched

  • preg_match($pattern, $subject, [array &$matches]): only matches once, the result is 0 or 1, the third parameter is optional, and the third parameter represents the reference of the address
  • preg_match($pattern, $subject, array &$matches): Match all, the result is 0,1,2...

eg:

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

 $subject = 'weuyr3ui76as83s0ck9';

 $m1 = $m2 = array();

t1 = grep_match($pattern, $subject, $m1);

t2 = grep_match_all($pattern, $subject, $m2);

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 array replacement

  • preg_replace($pattern, $replacement, $subject): retain the values ​​that have been replaced and those that have not been replaced
  • preg_filter($pattern, $replacement, $subject): retain the replaced value

eg one:

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

 $subject = 'weuyr3ui76as83s0ck9';

 $replacement = 'ying';

 $str1 = preg_replace($pattern, $replacement, $subject);

 $str2 = preg_filter($pattern, $replacement, $subject);

Result:

$str1 = 'weuyryinguiyingyingasyingyingsyingckying'

$str2 = 'weuyryinguiyingyingasyingyingsyingckying'

eg two:

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

 $replacement = array('Ah', 'La', '妦')

Result:

$str1 = 'weuyrah ui 妦拉as妦ah sah ck 妦'

 $str2 = 'weuyrahui妦拉as妦ahsahck妦'

eg three:

$subject = array('weuy', 'r3ui', '76as83', 's', '0ck9');

Result:

 $str1 = array([0]=>weuy, [1]=>rui, [2]=>妦拉as妦Ah, [3]=>s, [4]= >Ah ck 妦)

 $str2 = array([1]=>rui, [2]=>妦拉as妦妦ah, [4]=>ahck妦)

 (3) grep_grep($pattern, array $input): A emasculated version of grep_filter(), only matching, no replacement

eg:

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

$subject = array('weuy', 'r3ui', '76as83', 's', '0ck9');

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

Result:

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

 (4) grep_split($pattern, $subject): explode is a subset of this function

eg:

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

 $subject = 'You 2 are so good at 3!'

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

Result:

 $arr = ([0]=>You, [1]=>Okay, [2]=>Ah!)

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1109850.htmlTechArticleregular expression (php function), regularexpression 1. Regular expression is a tool for string search and matching 2 . Commonly used regular expression function preg_match($pattern, $subject) in php...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template