Function str_ireplace in PHP4


According to the document of PHP official website, http://php.net/str_ireplace

str_ireplace

(PHP 5)

str_ireplace — Case-insensitive version of str_replace().

So, it’s only for PHP5+. In PHP4, we use this function below:

function _str_ireplace($search, $replace, $subject)
{
    $temp = chr(1);
    $haystack = strtolower($subject);
    $needle = strtolower($search);
    while (($strpos = strpos($haystack, $needle)) !== false) {
        $subject = substr_replace($subject, $temp, $strpos, strlen($search));
        $haystack = substr_replace($haystack, $temp, $strpos, strlen($search));
    }
    $subject = str_replace($temp, $replace, $subject);
    return $subject;
}

function _str_ireplace2($search, $replace, $subject){
    if(is_array($search)){
        $search = array_values($search);
        if(is_array($replace)){
            $replace = array_values($replace);
            foreach($search as $key=>$value){
                $subject = _str_ireplace($value, isset($replace[$key])?$replace[$key]:"", $subject);
            }
        }else{
            foreach($search as $value){
                $subject = _str_ireplace($value, $replace, $subject);
            }
        }
        return $subject;
    }else{
        if(is_array($replace)){
            $replace = array_values($replace);
            return _str_ireplace($search, $replace[0], $subject);
        }else{
            return _str_ireplace($search, $replace, $subject);
        }
    }
}

function str_ireplace2($search, $replace, $subject){
    if(is_array($subject)){
        $return = array();
        foreach($subject as $value){
            $return[] = _str_ireplace2($search, $replace, $value);
        }
        return $return;
    }else{
        return _str_ireplace2($search, $replace, $subject);
    }
} 

if(!function_exists('str_ireplace')){
    function str_ireplace($search, $replace, $subject){
        return str_ireplace2($search, $replace, $subject);
    }
}

Example 1:

var_dump(
    str_ireplace2(
        array(
            'search',
            'replace',
            'changements',
        ),
        array(
            'find',
            'change',                        
            'replacements',
        ),
        array(
            'If search and replace are arrays',
            'then str_ireplace() takes a value from each array and uses them to search and replace on subject',
            'An array may be used to designate multiple changements',
        )
    )
);

result:

array(3) {
[0]=>
string(29) “If find and change are arrays”
[1]=>
string(92) “then str_ichange() takes a value from each array and uses them to find and change on subject”
[2]=>
string(55) “An array may be used to designate multiple replacements”
}

Example 2:

var_dump(
    str_ireplace2(
        array(
            'search',
            'replace',
            'changements',
        ),
        array(
            'change',                        
            'replacements',
        ),
        array(
            'If search and replace are arrays',
            'then str_ireplace() takes a value from each array and uses them to search and replace on subject',
            'An array may be used to designate multiple changements',
        )
    )
);

result:

array(3) {
[0]=>
string(37) “If change and replacements are arrays”
[1]=>
string(106) “then str_ireplacements() takes a value from each array and uses them to change and replacements on subject”
[2]=>
string(43) “An array may be used to designate multiple ”
}

Example 3:

var_dump(
    str_ireplace2(
        array(        
            'replace',
            'changements',
        ),
        array(
            'find',
            'change',                        
            'replacements',
        ),
        array(
            'If search and replace are arrays',
            'then str_ireplace() takes a value from each array and uses them to search and replace on subject',
            'An array may be used to designate multiple changements',
        )
    )
);

result:

array(3) {
[0]=>
string(29) “If search and find are arrays”
[1]=>
string(90) “then str_ifind() takes a value from each array and uses them to search and find on subject”
[2]=>
string(49) “An array may be used to designate multiple change”
}

Leave a Reply