[editor note] This example script is misleading. preg_last_error() only returns PREG_*_ERROR constants.
This function might come in handy to debug regular expressions in preg_.. calls
<?php
function pcre_error_deocde() {
switch (preg_last_error()) {
case PREG_PATTERN_ORDER:
print "Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. This flag is only used with preg_match_all().\n";
break;
case PREG_SET_ORDER:
print "Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. This flag is only used with preg_match_all().\n";
break;
case PREG_OFFSET_CAPTURE:
print "See the description of PREG_SPLIT_OFFSET_CAPTURE. This flag is available since PHP 4.3.0.\n";
break;
case PREG_SPLIT_NO_EMPTY:
print "This flag tells preg_split() to return only non-empty pieces.\n";
break;
case PREG_SPLIT_DELIM_CAPTURE:
print "This flag tells preg_split() to capture parenthesized expression in the delimiter pattern as well. This flag is available since PHP 4.0.5.\n";
break;
case PREG_SPLIT_OFFSET_CAPTURE:
print "If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return values in an array where every element is an array consisting of the matched string at offset 0 and its string offset within subject at offset 1. This flag is available since PHP 4.3.0 and is only used for preg_split().\n";
break;
case PREG_NO_ERROR:
// do not print in this case
//print "Returned by preg_last_error() if there were no errors. Available since PHP 5.2.0.\n";
break;
case PREG_INTERNAL_ERROR:
print "Returned by preg_last_error() if there was an internal PCRE error. Available since PHP 5.2.0.\n";
break;
case PREG_BACKTRACK_LIMIT_ERROR:
print "Returned by preg_last_error() if backtrack limit was exhausted. Available since PHP 5.2.0.\n";
break;
case PREG_RECURSION_LIMIT_ERROR:
print "Returned by preg_last_error() if recursion limit was exhausted. Available since PHP 5.2.0.\n";
break;
case PREG_BAD_UTF8_ERROR:
print "Returned by preg_last_error() if the last error was caused by malformed UTF-8 data (only when running a regex in UTF-8 mode). Available since PHP 5.2.0.\n";
break;
case PREG_BAD_UTF8_OFFSET_ERROR:
print "Returned by preg_last_error() if the offset didn't correspond to the begin of a valid UTF-8 code point (only when running a regex in UTF-8 mode). Available since PHP 5.3.0.\n";
break;
case PCRE_VERSION:
print "PCRE version and release date (e.g. '7.0 18-Dec-2006'). Available since PHP 5.2.4.\n";
break;
}
}
?>
Constantes pré-definidas
As contantes abaixo são definidas por esta extensão e somente estarão disponíveis quando a extensão foi compilada com o PHP ou carregada dinamicamente durante a execução.
| constant | description |
|---|---|
| PREG_PATTERN_ORDER | Ordena os resultados de modo que $matches[0] seja um array de todas as combinações do padrão. $matches[1] é um array de strings combinadas pelo primeiro subpadrão, e assim por diante. Esta flag é somente usada com preg_match_all(). |
| PREG_SET_ORDER | Ordena os resultados de modo que $matches[0] seja um array do primeiro conjunto de combinações, $matches[1] é um array do segundo conjunto de combinações, e assim por diante. Esta flag é somente usada com preg_match_all(). |
| PREG_OFFSET_CAPTURE | Veja a descrição da PREG_SPLIT_OFFSET_CAPTURE. Esta flag está disponível desde o PHP 4.3.0. |
| PREG_SPLIT_NO_EMPTY | Esta flag diz a preg_split() não retornar pedaços em branco. |
| PREG_SPLIT_DELIM_CAPTURE | Esta flag diz a preg_split() capturar expressões entre parênteses no delimitador do padrão também. Está flag está disponível desde o PHP 4.0.5. |
| PREG_SPLIT_OFFSET_CAPTURE | Se esta flag é usada, para cada combinação será retornada também a posição da string. Note que esta modificação retorna valores em um array onde cada elemento é um array consistindo da string combinada no índice 0 e a posição na string alvo na índice 1. Esta flag está disponível desde o PHP 4.3.0 e é somente usada por preg_split(). |
| PREG_NO_ERROR | Retornado pela preg_last_error() se não haver erros. Disponível desde o PHP 5.2.0. |
| PREG_INTERNAL_ERROR | Retornado pela preg_last_error() se houve um erro interno na PCRE. Disponível desde o PHP 5.2.0. |
| PREG_BACKTRACK_LIMIT_ERROR | Retornado pela preg_last_error() se backtrack limit foi esgotado. Disponível desde o PHP 5.2.0. |
| PREG_RECURSION_LIMIT_ERROR | Retornado pela preg_last_error() se recursion limit foi esgotado. Disponível desde o PHP 5.2.0. |
| PREG_BAD_UTF8_ERROR | Retornado pela preg_last_error() se o último erro foi causado por informação deformada em UTF-8 (somente quando usado a regex em modo UTF-8). Disponível desde o PHP 5.2.0. |
| PREG_BAD_UTF8_OFFSET_ERROR | Retornado pela preg_last_error() se o offset não correspondeu ao início de um válido code point UTF-8 (somente quando executando uma regex em modo UTF-8). Disponível desde o PHP 5.3.0. |
| PCRE_VERSION | Versão da PCRE e data de liberação (e.g. "7.0 18-Dec-2006"). Disponível desde o PHP 5.2.4. |
Constantes pré-definidas
Bastiaan
23-Jan-2009 11:22
23-Jan-2009 11:22
