downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/fr/function.preg-match.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/ref.pcre.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'fr',
  ),
 
'this' =>
  array (
   
0 => 'function.preg-match.php',
   
1 => 'preg_match',
  ),
 
'up' =>
  array (
   
0 => 'ref.pcre.php',
   
1 => 'Fonctions PCRE',
  ),
 
'prev' =>
  array (
   
0 => 'function.preg-match-all.php',
   
1 => 'preg_match_all',
  ),
 
'next' =>
  array (
   
0 => 'function.preg-quote.php',
   
1 => 'preg_quote',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="function.preg-match" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_match</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">preg_match</span> &mdash; <span class="dc-title">Expression rationnelle standard</span></p>

 </div>

 <a name="function.preg-match.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><b>preg_match</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$pattern</tt></span>
   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$subject</tt></span>
   [, <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&amp;$matches</tt></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span>
  ]]] )</div>

  <p class="para rdfs-comment">
   Analyse <i><tt class="parameter">subject</tt></i>
 pour trouver l&#039;expression qui
   correspond à <i><tt class="parameter">pattern</tt></i>
.
  </p>
 </div>


 <a name="function.preg-match.parameters"></a><div class="refsect1 parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   </p><dl>

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">pattern</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Le masque à chercher, sous la forme d&#039;une <a href="language.types.string.php" class="link">chaîne de caractères</a>.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">subject</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       La chaîne d&#039;entrée.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">matches</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Si <i><tt class="parameter">matches</tt></i>
 est fourni, il sera rempli par
       les résultats de la recherche. <var class="varname">$matches[0]</var> contiendra le
       texte qui satisfait le masque complet, <var class="varname">$matches[1]</var> contiendra
       le texte qui satisfait la première parenthèse capturante,
       etc.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">flags</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Le paramètre <i><tt class="parameter">flags</tt></i>
 peut prendre l&#039;une des valeurs
       suivantes :
       </p><dl>

        <dt class="varlistentry">

         <span class="term"><b><tt class="constant">PREG_OFFSET_CAPTURE</tt></b></span>

         </dt><dd class="listitem">

          <span class="simpara">
           Si cette option est activée, toutes les sous-chaînes qui satisfont
           le masque seront aussi identifiées par leur offset. Notez que cela
           modifie le format de la valeur retournée , puisque chaque élément
           de réponse devient un tableau contenant la sous-chaîne résultat,
           à l&#039;index <i>0</i> dans la chaîne <i><tt class="parameter">subject</tt></i>

           à l&#039;index <i>1</i>.
          </span>
         </dd>

       
       </dl>
<p>
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">offset</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Normalement, la recherche commence au début de la chaîne
       <i><tt class="parameter">subject</tt></i>
. Le paramètre optionnel
       <i><tt class="parameter">offset</tt></i>
 peut être utilisé pour spécifier
       une position pour le début de la recherche (en octets).
      </p>
      <blockquote><p><b class="note">Note</b>:
      
        Utiliser le paramètre <i><tt class="parameter">offset</tt></i>
 ne revient pas
        à passer <i>substr($subject, $offset)</i> à
        <a href="function.preg-match-all.php" class="function">preg_match_all()</a> à la place de la chaîne
        <i><tt class="parameter">subject</tt></i>
, car
        <i><tt class="parameter">pattern</tt></i>
 peut contenir des assertions comme
        <em class="emphasis">^</em>, <em class="emphasis">$</em> ou
        <em class="emphasis">(?&lt;=x)</em>.
        Comparez :
        </p><div class="informalexample">
         <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$subject&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pattern&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/^def/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$subject</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_OFFSET_CAPTURE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
         </div>

         <p class="para">L&#039;exemple ci-dessus va afficher :</p>
         <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
)
</pre></div>
         </div>
         <p class="para">
          avec cet exemple :
         </p>
         <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$subject&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pattern&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/^def/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$subject</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_OFFSET_CAPTURE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
         </div>

         <p class="para">
          produira :
         </p>
         <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; def
            [1] =&gt; 0
        )

)
</pre></div>
         </div>
        </div><p>
       <br />
      </p></blockquote>
     </dd>

   
   </dl>
<p>
  </p>
 </div>


 <a name="function.preg-match.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   <b>preg_match()</b> retourne le nombre de fois où le
   masque <i><tt class="parameter">pattern</tt></i>
 a été trouvé. Cela peut aller de
   0 (pas de solution) à un 1 car <b>preg_match()</b> s&#039;arrête
   dès qu&#039;elle a trouvé une première solution. <a href="function.preg-match-all.php" class="function">preg_match_all()</a>,
   au contraire, va continuer jusqu&#039;à la fin de la chaîne <i><tt class="parameter">subject</tt></i>
.
   <b>preg_match()</b> retourne <b><tt class="constant">FALSE</tt></b> si une erreur survient.
  </p>
 </div>


 <a name="function.preg-match.changelog"></a><div class="refsect1 changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   </p><table class="doctable informaltable">
   
     <thead valign="middle">
      <tr valign="middle">
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody valign="middle" class="tbody">
      <tr valign="middle">
       <td align="left">4.3.3</td>
       <td align="left">
        Le paramètre <i><tt class="parameter">offset</tt></i>
 a été ajouté.
       </td>
      </tr>

      <tr valign="middle">
       <td align="left">4.3.0</td>
       <td align="left">
        Le drapeau <b><tt class="constant">PREG_OFFSET_CAPTURE</tt></b> a été ajouté.
       </td>
      </tr>

      <tr valign="middle">
       <td align="left">4.3.0</td>
       <td align="left">
        Le paramètre <i><tt class="parameter">flags</tt></i>
 a été ajouté.
       </td>
      </tr>

     </tbody>
   
   </table>
<p>
  </p>
 </div>


 <a name="function.preg-match.examples"></a><div class="refsect1 examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   </p><div class="example">
    <p><b>Exemple #1 Trouve la chaîne &quot;php&quot;</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Le&nbsp;"i"&nbsp;après&nbsp;le&nbsp;délimiteur&nbsp;du&nbsp;pattern&nbsp;indique&nbsp;que&nbsp;la&nbsp;recherche&nbsp;ne&nbsp;sera&nbsp;pas&nbsp;sensible&nbsp;à&nbsp;la&nbsp;casse<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"/php/i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PHP&nbsp;est&nbsp;le&nbsp;meilleur&nbsp;langage&nbsp;de&nbsp;script&nbsp;du&nbsp;web."</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Un&nbsp;résultat&nbsp;a&nbsp;été&nbsp;trouvé."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Aucun&nbsp;résultat&nbsp;n'a&nbsp;été&nbsp;trouvé."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
  <p class="para">
   </p><div class="example">
    <p><b>Exemple #2 Trouve le mot &quot;web&quot;</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;\b,&nbsp;dans&nbsp;le&nbsp;masque,&nbsp;indique&nbsp;une&nbsp;limite&nbsp;de&nbsp;mot,&nbsp;de&nbsp;façon&nbsp;à&nbsp;ce&nbsp;que&nbsp;le&nbsp;mot<br />&nbsp;"web"&nbsp;uniquement&nbsp;soit&nbsp;repéré,&nbsp;et&nbsp;pas&nbsp;seulement&nbsp;des&nbsp;parties&nbsp;de&nbsp;mots&nbsp;comme<br />&nbsp;&nbsp;dans&nbsp;"webbing"&nbsp;ou&nbsp;"cobweb"&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"/\bweb\b/i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PHP&nbsp;est&nbsp;le&nbsp;meilleur&nbsp;langage&nbsp;de&nbsp;script&nbsp;du&nbsp;web."</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Le&nbsp;mot&nbsp;a&nbsp;été&nbsp;trouvé."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Le&nbsp;mot&nbsp;n'a&nbsp;pas&nbsp;été&nbsp;trouvé."</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"/\bweb\b/i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PHP&nbsp;est&nbsp;le&nbsp;meilleur&nbsp;langage&nbsp;de&nbsp;script&nbsp;du&nbsp;web."</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Le&nbsp;mot&nbsp;a&nbsp;été&nbsp;trouvé."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Le&nbsp;mot&nbsp;n'a&nbsp;pas&nbsp;été&nbsp;trouvé."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
  <p class="para">
   </p><div class="example">
    <p><b>Exemple #3 Lire un nom de domaine dans une URL</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;repérer&nbsp;le&nbsp;nom&nbsp;de&nbsp;l'hôte&nbsp;dans&nbsp;l'URL<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'@^(?:http://)?([^/]+)@i'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"http://www.php.net/index.html"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$host&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;repérer&nbsp;les&nbsp;deux&nbsp;derniers&nbsp;segments&nbsp;du&nbsp;nom&nbsp;de&nbsp;l'hôte<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/[^.]+\.[^.]+$/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$host</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Le&nbsp;nom&nbsp;de&nbsp;domaine&nbsp;est&nbsp;:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Le nom de domaine est : php.net
</pre></div>
    </div>
   </div><p>
  </p>
  <p class="para">
   </p><div class="example">
    <p><b>Exemple #4 Utilisation des sous-masques nommés</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foobar:&nbsp;2008'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/(?&lt;name&gt;\w+):&nbsp;(?&lt;digit&gt;\d+)/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; foobar: 2008
    [name] =&gt; foobar
    [1] =&gt; foobar
    [digit] =&gt; 2008
    [2] =&gt; 2008
)
</pre></div>
    </div>
   </div><p>
  </p>
 </div>


 <a name="function.preg-match.notes"></a><div class="refsect1 notes">
  <h3 class="title">Notes</h3>
  <div class="tip"><b class="tip">Astuce</b>
   <p class="para">
    N&#039;utilisez pas <b>preg_match()</b>  si vous voulez uniquement
    savoir si une chaîne est contenue dans une autre.
    Utilisez dans ce cas les fonctions
    <a href="function.strpos.php" class="function">strpos()</a> ou <a href="function.strstr.php" class="function">strstr()</a>,
    qui sont beaucoup plus rapides.
   </p>
  </div>
 </div>


 <a name="function.preg-match.seealso"></a><div class="refsect1 seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   </p><ul class="simplelist">
    <li class="member"><a href="function.preg-match-all.php" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - Expression rationnelle globale</li>
    <li class="member"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Rechercher et remplacer par expression rationnelle standard</li>
    <li class="member"><a href="function.preg-split.php" class="function" rel="rdfs-seeAlso">preg_split()</a> - Éclate une chaîne par expression rationnelle</li>
   </ul><p>
  </p>
 </div>


</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites