str_ireplace for php < 5

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

str_ireplace for php < 5

Post by s.dot »

continuing on from my post last night for str_split, here's another function i use since im running php < 5

Code: Select all

<?php

if(!function_exists('str_ireplace')){
	function str_ireplace($search,$replace,$subject){
		if(is_string($search) && is_string($replace) && is_string($subject)){
			return preg_replace("/$search/i",$replace,$subject);
		} elseif(is_string($search) && is_string($replace) && is_array($subject)){
			$ret = array();
			foreach($subject AS $v){
				$ret[] = preg_replace("/$search/i",$replace,$v);
			}
			return $ret;
		} elseif(is_array($search) && is_string($replace) && is_string($subject)){
			$ret = $subject;
			foreach($search AS $k){
				$ret = preg_replace("/$k/i",$replace,$ret);
			}
			return $ret;
		} elseif(is_array($search) && is_array($replace) && is_string($subject)){
			$ret = $subject;
			for($i=0;$i<count($search);$i++){
				if(!empty($replace[$i])){
					$ret = preg_replace("/{$search[$i]}/i",$replace[$i],$ret);
				} else {
					$ret = preg_replace("/{$search[$i]}/i",'',$ret);
				}
			}
			return $ret;
		}
	}
}

?>
I haven't tested it that much really, and im sure there's cases that I left out (can all of them be arrays?) but this works for simple stuff!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

In case you're looking for more back-to-the-future functions, i find http://pear.php.net/package/PHP_Compat quite useful (i thought i had seen non-pear version too, but can't find it back).
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Obscure bug

Code: Select all

echo str_ireplace('hi/', 'bye', 'hibye');
You must preg_quote()
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

My attempt:

Code: Select all

if(!function_exists('str_ireplace')){
    function str_ireplace($search, $replace, $subject){
 
       $ret = array();

        if(!is_array($subject))
            $subject = array($subject);
        $subject = array_values($subject);

        if(!is_array($search))
            $search = array($search);
        $search = array_values($search);

        if(is_array($replace))
            $replace = array_values($replace);

        foreach($subject as $subj) {

            $current = $subj;

            foreach($search as $offset => $srch) {
                if(is_array($replace)) {
                    $current = preg_replace('/' . preg_quote($srch, '/') . '/i', @$replace[$offset], $current);
                } else {
                    $current = preg_replace('/' . preg_quote($srch, '/') . '/i', $replace, $current);
                }
            }

            $ret[] = $current;

        }

        return count($ret) > 1 ? $ret : $ret[0];

    }
}
Unfortunately, it's impossible to support fourth argument to str_ireplace. There's no way to define optional argument to be passed by reference.
Post Reply