Zend - The PHP Company




Text

Add Code


Case insensitive str_replace  

Type: code fragment
Added by: stevo_x
Entered: 29/10/2002
Last modified: 09/12/2008
Rating: **** (3 votes)
Views: 6354
This function returns a string with all occurences of search in subject replaced with the given replace value.


<?php

function replacestring($search,$replace,$subject) {
    
$srchlen=strlen($search);    // lenght of searched string
    
    
while ($find stristr($subject,$search)) {    // find $search text in $subject - case insensitiv
        
$srchtxt substr($find,0,$srchlen);    // get new search text 
        
$subject str_replace($srchtxt,$replace,$subject);    // replace founded case insensitive search text with $replace
    
}
    return 
$subject;
}

?>


Usage Example


$a="Hello world, hello world, hello World, HelLo WOrld";

echo replacestring("hello","Hi",$a);


Rate This Script





Search



This Category All Categories