Maison php教程 php手册 php中preg正则函数使用

php中preg正则函数使用

Jun 21, 2016 am 08:47 AM
match str

1.preg_match和preg_match_all的区别
preg_match和 preg_match_all区别是preg_match只匹配一次。而preg_match_all全部匹配,直到字符串结束。例:
<?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)
 */
?>
Copier après la connexion
2.贪婪模式和非贪婪模式的区别
如:String str="abcaxc";
  Patter p="ab*c";
贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。
非贪婪匹配:就是匹配到结果就好,较少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
例:
<?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)
 */
?>
Copier après la connexion
3.preg_match_all参数PREG_PATTERN_ORDER(默认)和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)
 */
?>
Copier après la connexion

延伸阅读:preg_match_all使用实例



Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Où trouver la courte de la grue à atomide atomique
1 Il y a quelques semaines By DDD

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)