note 55612 added to function.str-ireplace
| From: | (support@ejdude.com) | Date: | Mon, 08 Aug 2005 20:34:00 +0000 |
| Subject: | note 55612 added to function.str-ireplace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93274@lists.php.net to get a copy of this message | ||
str_ireplace() for PHP <5
<?php
// mixed str_ireplace ( mixed search, mixed replace, mixed subject, int &count] )
// Case-insensitive version of str_replace.
// If someone knows how to pass the $count paramater as a reference without
// requiring it, please let me know. I tried func_get_arg() already. Lines
// to do with $count have been commented out by "// >>"
if ( !function_exists('str_ireplace') )
{
function str_ireplace ($search, $replace, $subject) // >> , &$count)
{
//Loop back until done if using a subject array
if ( is_array($subject) )
{
$array_keys = array_keys($subject);
while ( true )
{
// This is done here rather than in the while() statement because 0 evaluates as false
$key = array_pop($array_keys);
// If $key is null then there are no more subjects
if ( $key === NULL)
{
return $subject;
}
$subject[$key] = str_ireplace($search, $replace, $subject[$key]); // >> , $count);
}
return $subject;
}
// Loop back until done if using a search array
if ( is_array($search) )
{
//Only need to check this once
$is_array = is_array($replace);
while ( true )
{
$needle = array_pop($search);
// If needle is null then there are no more searches to process
if ( $needle === NULL )
{
return $subject;
}
// If no needle, don't even bother
// In PHP, empty(0) returns true, so the second check is needed
if ( empty($needle) && (string)$needle !== "0" )
{
continue;
}
if ( $is_array == true )
{
// If no replacements are left, use empty string instead
if ( count($replace) )
{
$replacement = array_pop($replace);
}
else
{
$replacement = '';
}
}
else
{
// If replacement is not an array, use the same value each time
$replacement = $replace;
}
$subject = str_ireplace($needle, $replacement, $subject); // >> , $count);
}
return $subject;
}
// Now begins the meat of the function
// Define some variables - compiler throws 'E_NOTICE undefined variable' if you
don't
$newsubject = '';
$end = '';
$offset = 0;
// Only need to do this once
$lowerneedle = strtolower($search);
$lowersubject = strtolower($subject);
// Keep going until I say so
while ( true )
{
// $pos will either be the position of the needle (can be 0) or FALSE
$pos = strpos($lowersubject, $lowerneedle, $offset);
// Operator === also checks against types, 0 == FALSE, 0 !== FALSE
if ($pos === false)
{
// Tack on the rest
$end = substr($subject, $offset);
$newsubject .= $end;
return $newsubject;
}
// Get the data before the needle
$front = substr($subject, $offset, $pos);
// Append the next piece and the replacement
$newsubject .= $front . $replace;
// Set offset to search beyond needle next time
// This is important because the replacement may contain the needle
$offset = $pos + strlen($search);
// Pretty self explanatory
// >> $count++;
}
// This should never happen, but it's here in case
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+55612
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55612&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55612&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55612&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55612&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55612&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55612&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55612&report=yes
Search -- http://master.php.net/manage/user-notes.php