note 78241 added to function.str-replace
| From: | hinom | Date: | Thu, 04 Oct 2007 05:32:22 +0000 |
| Subject: | note 78241 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131068@lists.php.net to get a copy of this message | ||
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 );
?>
----
Server IP: 219.94.145.73
Probable Submitter: 125.200.179.80
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/78241
Del: integrated -- https://master.php.net/note/delete/78241/integrated
Del: useless -- https://master.php.net/note/delete/78241/useless
Del: bad code -- https://master.php.net/note/delete/78241/bad+code
Del: spam -- https://master.php.net/note/delete/78241/spam
Del: non-english -- https://master.php.net/note/delete/78241/non-english
Del: in docs -- https://master.php.net/note/delete/78241/in+docs
Del: other reasons-- https://master.php.net/note/delete/78241
Reject -- https://master.php.net/note/reject/78241
Search -- https://master.php.net/manage/user-notes.php