Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    str_ireplace function..

    I'm using the below function in place of the "str_ireplace" function since I do not have PHP5 installed..

    PHP Code:
    if ( !function_exists('str_ireplace') )
    {
       function 
    str_ireplace ($search$replace$subject// >> , &$count)
       
    {
           
    //Loop back until done if using a subject array
           
    if ( is_array($subject) )
           {
               
    $array_keys array_keys($subject);
               while ( 
    true )
               {
                   
    // This is done here rather than in the while() statement because 0 evaluates as false
                   
    $key array_pop($array_keys);
                   
                   
    // If $key is null then there are no more subjects
                   
    if ( $key === NULL)
                   {
                       return 
    $subject;
                   }
               
                   
    $subject[$key] = str_ireplace($search$replace$subject[$key]); // >> , $count);
               
    }
               return 
    $subject;
           }
       
           
    // Loop back until done if using a search array        
           
    if ( is_array($search) )
           {
               
    //Only need to check this once
               
    $is_array is_array($replace);
               
               while ( 
    true )
               {
                   
    $needle array_pop($search);
                   
                   
    // If needle is null then there are no more searches to process
                   
    if ( $needle === NULL )
                   {
                       return 
    $subject;
                   }
                   
                   
    // If no needle, don't even bother
                   // In PHP, empty(0) returns true, so the second check is needed
                   
    if ( empty($needle) && (string)$needle !== "0" )
                   {
                       continue;
                   }
                   
                   if ( 
    $is_array == true )
                   {
                       
    // If no replacements are left, use empty string instead
                       
    if ( count($replace) )
                       {
                           
    $replacement array_pop($replace);
                       }
                       else
                       {
                           
    $replacement '';
                       }
                   }
                   else
                   {
                       
    // If replacement is not an array, use the same value each time
                       
    $replacement $replace;
                   }
                   
                   
    $subject str_ireplace($needle$replacement$subject); // >> , $count);
               
    }
               return 
    $subject;
           }
       
           
    // Now begins the meat of the function
           // Define some variables - compiler throws 'E_NOTICE undefined variable' if you don't
           
    $newsubject '';
           
    $end '';
           
    $offset 0;
       
           
    // Only need to do this once
           
    $lowerneedle strtolower($search);
           
    $lowersubject strtolower($subject);
       
           
    // Keep going until I say so
           
    while ( true )
           {
               
    // $pos will either be the position of the needle (can be 0) or FALSE
               
    $pos strpos($lowersubject$lowerneedle$offset);
               
    // Operator === also checks against types, 0 == FALSE, 0 !== FALSE
               
    if ($pos === false)
               {
                   
    // Tack on the rest
                   
    $end substr($subject$offset);
                   
    $newsubject .= $end;
                   return 
    $newsubject;
               }
               
               
    // Get the data before the needle
               
    $front substr($subject$offset$pos);
               
    // Append the next piece and the replacement
               
    $newsubject .= $front $replace;
               
    // Set offset to search beyond needle next time
               // This is important because the replacement may contain the needle
               
    $offset $pos strlen($search);
               
    // Pretty self explanatory
               // >> $count++;
           
    }
       
           
    // This should never happen, but it's here in case
           
    return $subject;
       }

    Now.... i am using this code for example..

    PHP Code:
    $st str_ireplace($bterms"<b>$bterms</b>"$title); 
    ..but if the word contained inside $bterms has the first letter uppercased this function always returns the result in all lowercase chars... I also tried to do it with preg_replace() but got the same result.

    I know I could just do this afterwards..

    PHP Code:
    $st ucfirst($st); 
    ..but I don't want to do that because the word may not always be uppercased in the first place..

    Any solutions?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    ..but if the word contained inside $bterms has the first letter uppercased this function always returns the result in all lowercase chars... I also tried to do it with preg_replace() but got the same result.
    Woops.... I made a mistake. I meant that if any of the found results in $title began with an uppercase character then the returned result would be in all lowercase seeing as whatever input it gets it displays that rather than the what wa sorginally inside $title..... it basically just displays what $bterms holds..... but I want ot to output the correct format of the found word inside of $title but can't work out how to do it?

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    str_ireplace doesn't preserve the case. You would have to get the case from the string... It should be doable with ASCII...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    This should work.
    PHP Code:
    <?php
    $search 
    = array('fooo''bar');
    $replace = array('bar''foo');
    $string 'Some Fooo is better than BaRr';
    print 
    sstr_ireplace($search$replace$string);
    function 
    sstr_ireplace($search$replace$string)
    // the extra s is for "special"
        
    if(is_array($search))
        {
            
    $input implode('|'$search);
        }
        else
        {
            
    $input $search;
            
    $search = array($search);
        }
        if(!
    is_array($replace))
        {
            
    $replace = array($replace);
        }
        
    // Let's get make a search => replace array so we won't have to array_search().
        
    $map = array();
        for(
    $i 0$n count($search), $m count($replace); $i $n$i++)
        {
            
    $map[strtolower($search[$i])] = strtolower($replace[($i $m)]);
        }
        
    $result explode(chr(0), preg_replace('/(' $input ')/i'chr(0) . '\\1' chr(0), str_replace(chr(0), ''$string)));
        for(
    $i 0$n count($result); $i $n$i++)
        {
            if(
    $i 2)
            {
                
    $uc $result[$i];
                
    $lc strtolower($result[$i]);
                
    $result[$i] = $map[$lc];
                for(
    $j 0$m strlen($uc); $j $m$j++)
                { 
    // The if below hopes that indexes can be anything :)
                    
    if(!isset($result[$i]{$j}))
                    { 
    // No need to go on if $replace is shorter than $search.
                        
    break;
                    }
                    if((
    $uc{$j} != $lc{$j}))
                    { 
    // Different case. Testing showed that == is case sensitive... it can be done in another way if it is only on my PHPversion.
                        
    $result[$i]{$j} = strtoupper($result[$i]{$j});
                    }
                }
            }
        }
        return 
    implode(''$result);
    }
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks a lot Marek but it didn't work.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It didn't? Have anything else to say? It works for me.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Try this:
    PHP Code:
    <?php
    if(!function_exists('str_ireplace')) { 
        function 
    str_ireplace($search$replace$subject, &$count 0) { 
            if(
    is_array($subject)) { 
                foreach(
    $subject as &$subj) { 
                    
    $subj str_ireplace($search$replace$subj$count); 
                } 
                return 
    $subject
            } 
            if(
    is_array($search)) { 
                
    $isReplArr is_array($replace); 
                if(!
    $isReplArr$replacement $replace
                while((
    $needle array_pop($search)) !== NULL) { 
                    if(
    $isReplArr) { 
                        if(empty(
    $replace)) $replacement ''
                        else 
    $replacement array_pop($replace); 
                    } 
                    
    $subject str_ireplace($needle$replacement$subject$count); 
                } 
                return 
    $subject
            } 
            if((
    $pos stripos($subject$search)) === FALSE) return $subject
            ++
    $count
            return 
    substr($subject0$pos).$replace.substr($subject$pos strlen($search)); 
        } 

    ?>
    dumpfi
    Last edited by dumpfi; 09-10-2005 at 02:41 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •