<?php

    
/**
     * Replaces text using PCRE or Extended POSIX functions
     *
     * This method is handy to seamlessly support whichever regular expression
     * function is compiled into PHP. Preferably, it will use PCRE function.
     * If you need the power of Perl-compatible functions then use the preg_*
     * functions, and mark your package as dependent on PCRE...
     *
     * Example of usage:
     * <code>
     * $text = '123a ABC abc #@';
     * $p = array(
     *          'preg' => array(
     *              '/[a-z]/',
     *              '/\b\w+\b/'),
     *          'ereg' => array(
     *              '[[:lower:]]',
     *              '[[:<:]][[:alnum:]]+[[:>:]]'));
     *
     * $r = array(
     *     '-', 'word');
     *
     * echo reg_replace($p, $r, $text, true) ."\r\n";
     * echo reg_replace('[0-9]', '0', $text) ."\r\n";
     *
     * // Result:
     * word- word --- #@
     * 000a ABC abc #@
     * <code>
     *
     * @param mixed $pattern associative array with two indexes "preg" and "ereg"
     *              Each value can also be an array of regular expression to use
     *              If "preg" and "ereg" indexes are not used, "/" will be
     *              used to encapsulate the regular expression for PCRE.
     * @param mixed $replacement If a string or an array without the "preg" or
     *              "ereg" index, the same replacement will be done
     *              regardless of which PCRE or Extended-POSIX function is used
     * @param mixed $subject array of string or simple string
     * @param bool  $case TRUE for case-sensitive matching, FALSE for
     *              case-insensitive
     * @return mixed the subject with the patterns replaced
     * @static
     * @access public
     * @see preg_replace()
     * @see ereg_replace()
     * @see eregi_replace()
     * @author Philippe -dot- Jausions -at- 11abacus -dot- com
     */
    
function reg_replace($pattern$replacement$subject$case TRUE)
    {
        if (
function_exists('preg_replace')) {
            if (
is_array($replacement) && isset($replacement['preg'])) {
                
$replacement $replacement['preg'];
            }
            
$case = ($case) ? '' 'i';
            if (isset(
$pattern['preg'])) {
                
$pattern $pattern['preg'];
            } elseif (
is_array($pattern)) {
                foreach (
$pattern as $i => $p) {
                    
$pattern[$i] = '/' $p  '/' $case;
                }
            } else {
                
$pattern '/' $pattern '/' $case;
            }
            return 
preg_replace(
                
$pattern,
                
$replacement,
                
$subject);
        }
        if (
is_array($replacement) && isset($replacement['ereg'])) {
            
$replacement $replacement['ereg'];
        }
        if (isset(
$pattern['ereg'])) {
            
$pattern $pattern['ereg'];
        }
        
$ereg_function = ($case) ? 'ereg_replace' 'eregi_replace';
        if (
is_array($subject)) {
            
$result = array();
            foreach (
$subject as $index => $value) {
                
$result[$index] = reg_replace($pattern$replacement$value$case);
            }
            return 
$result;
        } elseif (
is_array($pattern) && is_array($replacement)) {
            
$result $subject;
            foreach (
$pattern as $i => $p) {
                
$result $ereg_function($p, @$replacement[$i], $result);
            }
            return 
$result;
        } elseif (
is_array($pattern)) {
            
$result $subject;
            foreach (
$pattern as $p) {
                
$result $ereg_function($p$replacement$result);
            }
            return 
$result;
        }
        return 
$ereg_function($pattern$replacement$subject);
    }

?>