Use of preg regular function in php_PHP tutorial

WBOY
Release: 2016-07-13 10:17:19
Original
1058 people have browsed it

Using preg regular function in php

1.The difference between preg_match and preg_match_all
The difference between preg_match and preg_match_all is that preg_match only matches once. And preg_match_all matches everything until the end of the string. Example:
<?php
//注:正则 /a.+?e/ 是非贪婪模式(因为量词‘+’后面加上了‘?’),如果使用 /a.+?e/U 则变回了贪婪模式
preg_match("/a.+?e/","abcdefgabcdefgabcdefg",$out1);
preg_match_all("/a.+?e/","abcdefgabcdefgabcdefg",$out2);
var_dump($out1);
var_dump($out2);
/*
输出:
array (size=1)
  0 => string &#39;abcde&#39; (length=5)

array (size=1)
  0 =>
    array (size=3)
      0 => string &#39;abcde&#39; (length=5)
      1 => string &#39;abcde&#39; (length=5)
      2 => string &#39;abcde&#39; (length=5)
 */
?>
Copy after login
2. The difference between greedy mode and non-greedy mode
Such as: String str="abcaxc";
Patter p="ab*c";
Greedy matching: Regular expressions generally tend to match to the maximum length, which is the so-called greedy matching. For example, if the pattern p is used to match the string str, the result is: abcaxc(ab*c).
Non-greedy matching: Just match the result, with fewer matching characters. For example, if the pattern p is used to match the string str, the result is: abc(ab*c).
Example:
<?php
$str = "http://www.baidu/.com?url=www.sina.com/";
preg_match("/http:(.*)com/", $str, $matches1); //贪婪匹配
var_dump($matches1);

preg_match("/http:(.*?)com/", $str, $matches2); //非贪婪匹配(量词&#39;*&#39;后面跟上了&#39;?&#39;)
var_dump($matches2);

/*
array (size=2)
  0 => string &#39;http://www.baidu/.com?url=www.sina.com&#39; (length=38)
  1 => string &#39;//www.baidu/.com?url=www.sina.&#39; (length=30)

array (size=2)
  0 => string &#39;http://www.baidu/.com&#39; (length=21)
  1 => string &#39;//www.baidu/.&#39; (length=13)
 */
?>
Copy after login
3. The difference between preg_match_all parameters PREG_PATTERN_ORDER (default) and PREG_SET_ORDER
<?php
echo(&#39;PREG_PATTERN_ORDER&#39;);
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
    "<b>start: </b><b>this is a test</b><b>end</b>",
    $out1);
var_dump($out1);

echo(&#39;PREG_SET_ORDER&#39;);
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
    "<b>start: </b><b>this is a test</b><b>end</b>",
    $out2, PREG_SET_ORDER);
var_dump($out2);

/*
PREG_PATTERN_ORDER
array (size=2)
  0 =>
    array (size=3)
      0 => string &#39;<b>start: </b>&#39; (length=14)
      1 => string &#39;<b>this is a test</b>&#39; (length=21)
      2 => string &#39;<b>end</b>&#39; (length=10)
  1 =>
    array (size=3)
      0 => string &#39;start: &#39; (length=7)
      1 => string &#39;this is a test&#39; (length=14)
      2 => string &#39;end&#39; (length=3)

PREG_SET_ORDER
array (size=3)
  0 =>
    array (size=2)
      0 => string &#39;<b>start: </b>&#39; (length=14)
      1 => string &#39;start: &#39; (length=7)
  1 =>
    array (size=2)
      0 => string &#39;<b>this is a test</b>&#39; (length=21)
      1 => string &#39;this is a test&#39; (length=14)
  2 =>
    array (size=2)
      0 => string &#39;<b>end</b>&#39; (length=10)
      1 => string &#39;end&#39; (length=3)
 */
?>
Copy after login

Extended reading: preg_match_all usage examples

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/894184.htmlTechArticleUsing preg regular function in php 1. The difference between preg_match and preg_match_all The difference between preg_match and preg_match_all is that preg_match only matches once. And preg_match_all matches all until the string ends...
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