note 78241 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 06 Jan 2008 12:45:36 +0000 |
| Subject: | note 78241 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134866@lists.php.net to get a copy of this message | ||
Note Submitter: hinom [at] hotmail [dot] co [dot] jp
----
recursive e case-insensitive str_replace for php users under 5 version
stripos() function is available in php5, therefore needed create generic function stripos2()
hope this help many users
best regards for all PHP!
thnks for zend and php developers
<?php
$find = "script"; // string to search
$new = "##"; // string to replace
// text
$string = "33onSCRIPTon oano AoKO k SCRIPT--atooto Atoto SCRIPTmoomo lko KOx0 lolo
POpopo";
function stripos2($string,$word){
$retval = false;
$word_len = strlen($word);
for($i=0;$i<=strlen($string);$i++){
if (strtolower(substr($string,$i,$word_len)) == strtolower($word)){
$retval[0] = true;
$retval[1] = $i;
$retval[2] = $word_len;
}
}
return $retval;
}
function striReplace( $string, $find, $new ){
$r = false;
while( $p = stripos2( $string, $find ) ){
if( $p[0] ){
$r = substr( $string, 0, $p[1] );
$r .= $new;
$r .= substr( $string, $p[1] + $p[2] );
}
$string = $r;
}
return $r;
}
echo 'original:<br />' . $string . '<hr>';
echo striReplace( $string, $find, $new );
?>