Zend - The PHP Company




Text

Add Code


replacestring (new)  

Type: code fragment
Added by: stevo_x
Entered: 30/10/2002
Last modified: 01/11/2001
Rating: - (fewer than 3 votes)
Views: 4064
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
    
if ($srchlen==0) return $subject;
    
$find $subject;
    while (
$find stristr($find,$search)) {    // find $search text in $subject - case insensitiv
        
$srchtxt substr($find,0,$srchlen);    // get new search text 
        
$find=substr($find,$srchlen);
        
$subject str_replace($srchtxt,$replace,$subject);    // replace founded case insensitive search text with $replace
    
}
    return 
$subject;
}

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

echo 
replacestring("hello","<b>hello</b>",$a);

?>


Usage Example




Rate This Script





Search



This Category All Categories