note 56930 added to function.str-ireplace
| From: | kafoma at gmail dot com | Date: | Mon, 19 Sep 2005 19:45:54 +0000 |
| Subject: | note 56930 added to function.str-ireplace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95427@lists.php.net to get a copy of this message | ||
For those of us still working un PHP<5 ..
However, this function only accept strings as parameters
function str_ireplace($search, $replace, $subject)
{
//Se inicializan las variables
$newsubject = '';
$end = '';
$offset = 0;
// Se convierten a minusculas
$lowerneedle = strtolower($search);
$lowersubject = strtolower($subject);
//Ciclo que termina cuando ya no se encuentre la busqueda en la cadena
while ( true )
{
// $pos tendra la posicion de search en subject (puede ser 0) o FALSE
$pos = strpos($lowersubject, $lowerneedle, $offset);
// Se comprueba con === si $pos es falso (no se encontro la cadena)
if ($pos === false)
{
//Se agrega el resto de la cadena a newsubject
$end = substr($subject, $offset);
$newsubject .= $end;
return $newsubject;
}
//Se definen cuantos caracteres se agregan
$posi = $pos - $offset;
// Se obtiene la parte de la cadena que no tiene $search
$front = substr($subject, $offset, $posi);
// Se agrega a la nueva cadena y se agrega $replace
$newsubject .= $front . $replace;
// Se define offset para que busque a partir de donde se quedo
// Es importante porque $replace puede contener $search
$offset = $pos + strlen($search);
}
// Por si acaso...
return $subject;
}
----
Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56930
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56930&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56930&report=yes
Search -- http://master.php.net/manage/user-notes.php