Back to index

php5  5.3.10
Defines | Functions | Variables
php_ereg.h File Reference
#include "php_regex.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define phpext_ereg_ptr   &ereg_module_entry
#define EREG(v)   (ereg_globals.v)

Functions

PHPAPI char * php_ereg_replace (const char *pattern, const char *replace, const char *string, int icase, int extended)
 PHP_FUNCTION (ereg)
 PHP_FUNCTION (eregi)
 PHP_FUNCTION (eregi_replace)
 PHP_FUNCTION (ereg_replace)
 PHP_FUNCTION (split)
 PHP_FUNCTION (spliti)
PHPAPI PHP_FUNCTION (sql_regcase)
 PHP_MINIT_FUNCTION (ereg)
 PHP_MSHUTDOWN_FUNCTION (ereg)
 PHP_MINFO_FUNCTION (ereg)

Variables

zend_module_entry ereg_module_entry
HashTable ht_rc
unsigned int lru_counter

Define Documentation

#define EREG (   v)    (ereg_globals.v)

Definition at line 53 of file php_ereg.h.

Definition at line 28 of file php_ereg.h.


Function Documentation

PHPAPI char* php_ereg_replace ( const char *  pattern,
const char *  replace,
const char *  string,
int  icase,
int  extended 
)

Definition at line 403 of file ereg.c.

{
       regex_t re;
       regmatch_t *subs;

       char *buf,    /* buf is where we build the replaced string */
               *nbuf,       /* nbuf is used when we grow the buffer */
               *walkbuf; /* used to walk buf when replacing backrefs */
       const char *walk; /* used to walk replacement string for backrefs */
       int buf_len;
       int pos, tmp, string_len, new_l;
       int err, copts = 0;

       string_len = strlen(string);

       if (icase) {
              copts = REG_ICASE;
       }
       if (extended) {
              copts |= REG_EXTENDED;
       }

       err = regcomp(&re, pattern, copts);
       if (err) {
              php_ereg_eprint(err, &re);
              return ((char *) -1);
       }


       /* allocate storage for (sub-)expression-matches */
       subs = (regmatch_t *)ecalloc(sizeof(regmatch_t),re.re_nsub+1);

       /* start with a buffer that is twice the size of the stringo
          we're doing replacements in */
       buf_len = 2 * string_len + 1;
       buf = safe_emalloc(buf_len, sizeof(char), 0);

       err = pos = 0;
       buf[0] = '\0';
       while (!err) {
              err = regexec(&re, &string[pos], re.re_nsub+1, subs, (pos ? REG_NOTBOL : 0));

              if (err && err != REG_NOMATCH) {
                     php_ereg_eprint(err, &re);
                     efree(subs);
                     efree(buf);
                     regfree(&re);
                     return ((char *) -1);
              }

              if (!err) {
                     /* backref replacement is done in two passes:
                        1) find out how long the string will be, and allocate buf
                        2) copy the part before match, replacement and backrefs to buf

                        Jaakko Hyvätti <Jaakko.Hyvatti@iki.fi>
                        */

                     new_l = strlen(buf) + subs[0].rm_so; /* part before the match */
                     walk = replace;
                     while (*walk) {
                            if ('\\' == *walk && isdigit((unsigned char)walk[1]) && ((unsigned char)walk[1]) - '0' <= (int)re.re_nsub) {
                                   if (subs[walk[1] - '0'].rm_so > -1 && subs[walk[1] - '0'].rm_eo > -1) {
                                          new_l += subs[walk[1] - '0'].rm_eo - subs[walk[1] - '0'].rm_so;
                                   }
                                   walk += 2;
                            } else {
                                   new_l++;
                                   walk++;
                            }
                     }
                     if (new_l + 1 > buf_len) {
                            buf_len = 1 + buf_len + 2 * new_l;
                            nbuf = emalloc(buf_len);
                            strncpy(nbuf, buf, buf_len-1);
                            nbuf[buf_len - 1] = '\0';
                            efree(buf);
                            buf = nbuf;
                     }
                     tmp = strlen(buf);
                     /* copy the part of the string before the match */
                     strncat(buf, &string[pos], subs[0].rm_so);

                     /* copy replacement and backrefs */
                     walkbuf = &buf[tmp + subs[0].rm_so];
                     walk = replace;
                     while (*walk) {
                            if ('\\' == *walk && isdigit((unsigned char)walk[1]) && ((unsigned char)walk[1]) - '0' <= (int)re.re_nsub) {
                                   if (subs[walk[1] - '0'].rm_so > -1 && subs[walk[1] - '0'].rm_eo > -1
                                          /* this next case shouldn't happen. it does. */
                                          && subs[walk[1] - '0'].rm_so <= subs[walk[1] - '0'].rm_eo) {
                                          
                                          tmp = subs[walk[1] - '0'].rm_eo - subs[walk[1] - '0'].rm_so;
                                          memcpy (walkbuf, &string[pos + subs[walk[1] - '0'].rm_so], tmp);
                                          walkbuf += tmp;
                                   }
                                   walk += 2;
                            } else {
                                   *walkbuf++ = *walk++;
                            }
                     }
                     *walkbuf = '\0';

                     /* and get ready to keep looking for replacements */
                     if (subs[0].rm_so == subs[0].rm_eo) {
                            if (subs[0].rm_so + pos >= string_len) {
                                   break;
                            }
                            new_l = strlen (buf) + 1;
                            if (new_l + 1 > buf_len) {
                                   buf_len = 1 + buf_len + 2 * new_l;
                                   nbuf = safe_emalloc(buf_len, sizeof(char), 0);
                                   strncpy(nbuf, buf, buf_len-1);
                                   efree(buf);
                                   buf = nbuf;
                            }
                            pos += subs[0].rm_eo + 1;
                            buf [new_l-1] = string [pos-1];
                            buf [new_l] = '\0';
                     } else {
                            pos += subs[0].rm_eo;
                     }
              } else { /* REG_NOMATCH */
                     new_l = strlen(buf) + strlen(&string[pos]);
                     if (new_l + 1 > buf_len) {
                            buf_len = new_l + 1; /* now we know exactly how long it is */
                            nbuf = safe_emalloc(buf_len, sizeof(char), 0);
                            strncpy(nbuf, buf, buf_len-1);
                            efree(buf);
                            buf = nbuf;
                     }
                     /* stick that last bit of string on our output */
                     strlcat(buf, &string[pos], buf_len);
              }
       }

       /* don't want to leak memory .. */
       efree(subs);
       regfree(&re);

       /* whew. */
       return (buf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHP_FUNCTION ( ereg  )

Definition at line 387 of file ereg.c.

Here is the call graph for this function:

PHP_FUNCTION ( eregi  )

Definition at line 395 of file ereg.c.

Here is the call graph for this function:

PHP_FUNCTION ( eregi_replace  )

Definition at line 621 of file ereg.c.

Here is the call graph for this function:

PHP_FUNCTION ( ereg_replace  )

Definition at line 613 of file ereg.c.

Here is the call graph for this function:

Definition at line 713 of file ereg.c.

Here is the call graph for this function:

PHP_FUNCTION ( spliti  )

Definition at line 722 of file ereg.c.

Here is the call graph for this function:

PHPAPI PHP_FUNCTION ( sql_regcase  )

Definition at line 731 of file ereg.c.

{
       char *string, *tmp;
       int string_len;
       unsigned char c;
       register int i, j;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) {
              return;
       }
       
       tmp = safe_emalloc(string_len, 4, 1);
       
       for (i = j = 0; i < string_len; i++) {
              c = (unsigned char) string[i];
              if (isalpha(c)) {
                     tmp[j++] = '[';
                     tmp[j++] = toupper(c);
                     tmp[j++] = tolower(c);
                     tmp[j++] = ']';
              } else {
                     tmp[j++] = c;
              }
       }
       tmp[j] = 0;

       RETVAL_STRINGL(tmp, j, 1);
       efree(tmp);
}

Here is the call graph for this function:

PHP_MINFO_FUNCTION ( ereg  )

Definition at line 232 of file ereg.c.

{
       php_info_print_table_start();
#if HSREGEX
       php_info_print_table_row(2, "Regex Library", "Bundled library enabled");
#else
       php_info_print_table_row(2, "Regex Library", "System library enabled");
#endif
       php_info_print_table_end();
}

Here is the call graph for this function:

PHP_MINIT_FUNCTION ( ereg  )

Definition at line 217 of file ereg.c.

Here is the call graph for this function:

Definition at line 223 of file ereg.c.

{
#ifndef ZTS
       php_ereg_destroy_globals(&ereg_globals TSRMLS_CC);
#endif

       return SUCCESS;
}

Here is the call graph for this function:


Variable Documentation

zend_module_entry ereg_module_entry

Definition at line 79 of file ereg.c.

Definition at line 41 of file php_ereg.h.

unsigned int lru_counter

Definition at line 42 of file php_ereg.h.