PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Oct 19, 2019 11:02 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: str_ireplace for php < 5
PostPosted: Sat Mar 25, 2006 1:47 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
continuing on from my post last night for str_split, here's another function i use since im running php < 5

Syntax: [ Download ] [ Hide ]
<?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!

_________________
- 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.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 25, 2006 3:24 am 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
In case you're looking for more back-to-the-future functions, i find quite useful (i thought i had seen non-pear version too, but can't find it back).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 11:56 am 
Offline
Forum Contributor

Joined: Sun Dec 26, 2004 10:04 pm
Posts: 110
Obscure bug

Syntax: [ Download ] [ Hide ]
echo str_ireplace('hi/', 'bye', 'hibye');


You must preg_quote()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 12:35 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
My attempt:
Syntax: [ Download ] [ Hide ]
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group